我想创建一个通用的布局,它接受一个子小部件作为参数,并按以下方式布置内容:
顶部有一个AppBar,一个标题(大标题),下面是内容(可以是任何内容)。底部有一个带有几个按钮的列。如果内容太大而无法适应屏幕,则除了AppBar之外的所有小部件都可以滚动。如果内容适合屏幕,则标题和内容应该对齐在顶部,按钮在底部。
为了展示我的意思,我创建了一张图纸: 很容易创建可滚动内容功能。但如果内容不需要滚动,我会遇到排版内容使按钮底部对齐的困难。
重要的是说,我不知道内容小部件或按钮的高度。它们是动态的,可以改变它们的高度。此外,标题是可选的,可以有两种不同的大小。
我尝试了以下方法:
导入 'package:flutter/material.dart';
滚动效果完美,但按钮没有对齐到底部。相反,它们直接对齐在内容下方。有人知道我该如何修复吗?
顶部有一个AppBar,一个标题(大标题),下面是内容(可以是任何内容)。底部有一个带有几个按钮的列。如果内容太大而无法适应屏幕,则除了AppBar之外的所有小部件都可以滚动。如果内容适合屏幕,则标题和内容应该对齐在顶部,按钮在底部。
为了展示我的意思,我创建了一张图纸: 很容易创建可滚动内容功能。但如果内容不需要滚动,我会遇到排版内容使按钮底部对齐的困难。
重要的是说,我不知道内容小部件或按钮的高度。它们是动态的,可以改变它们的高度。此外,标题是可选的,可以有两种不同的大小。
我尝试了以下方法:
导入 'package:flutter/material.dart';
class BaseScreen extends StatelessWidget {
final String? title;
final bool bigHeader;
final Widget child;
final Widget bottomButtons;
const BaseScreen({
Key? key,
required this.child,
required this.bottomButtons,
this.bigHeader = true,
this.title,
}) : super(key: key);
@override
Widget build(BuildContext context) {
final AppBar appBar = AppBar(
title: Text("AppBar"),
);
double minChildHeight = MediaQuery.of(context).size.height -
MediaQuery.of(context).viewInsets.bottom -
MediaQuery.of(context).viewInsets.top -
MediaQuery.of(context).viewPadding.bottom -
MediaQuery.of(context).viewPadding.top -
appBar.preferredSize.height;
if (title != null) {
minChildHeight -= 20;
if (bigHeader) {
minChildHeight -= bigHeaderStyle.fontSize!;
} else {
minChildHeight -= smallHeaderStyle.fontSize!;
}
}
final Widget content = Column(
mainAxisSize: MainAxisSize.min,
children: [
if (title != null)
Text(
title!,
style: bigHeader ? bigHeaderStyle : smallHeaderStyle,
textAlign: TextAlign.center,
),
if (title != null)
const SizedBox(
height: 20,
),
ConstrainedBox(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
child,
bottomButtons,
],
),
constraints: BoxConstraints(
minHeight: minChildHeight,
),
),
],
);
return Scaffold(
appBar: appBar,
body: SingleChildScrollView(
child: content,
),
);
}
TextStyle get bigHeaderStyle {
return TextStyle(fontSize: 20);
}
TextStyle get smallHeaderStyle {
return TextStyle(fontSize: 16);
}
}
滚动效果完美,但按钮没有对齐到底部。相反,它们直接对齐在内容下方。有人知道我该如何修复吗?