【发布时间】:2021-11-11 09:13:06
【问题描述】:
我在 Scaffold 中有一个简单的表单。 LoginPage 小部件是应用脚手架的直接子代:
class LoginPage extends StatefulWidget {
const LoginPage({Key? key}) : super(key: key);
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final _formKey = GlobalKey<FormState>();
final _userCtl = TextEditingController();
final _passCtl = TextEditingController();
@override
Widget build(BuildContext context) {
var h = MediaQuery.of(context).size.height;
var w = MediaQuery.of(context).size.width;
return Column(
children: [
Expanded(
flex: 1,
child: Container(),
),
Expanded(
flex: 2,
child: Container(
padding: const EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(width: 1, color: Colors.grey),
borderRadius: const BorderRadius.all(Radius.circular(30))),
width: w > breakPoint800 ? 0.5 * w : w,
height: 0.5 * h,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Form(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
children: [
const Text('Login',
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 24)),
UsernameInput(_userCtl), // TextFormField
PasswordInput(_passCtl), // Row of TextFormField and IconButton
],
),
ElevatedButton(
autofocus: true,
onPressed: () {
// Some logic here
}
},
child: const Text('Submit'),
),
],
),
),
),
),
),
Expanded(
flex: 1,
child: Container(),
)
],
);
}
}
我将 Column 包装在 SingleChildScrollView 中的原因是使其可滚动并消除窄屏幕高度下的 RenderBox 错误。
滚动问题现已解决,但mainAxisAlignment: MainAxisAlignment.spaceBetween 不起作用。即所有列的孩子都粘在一起。似乎 SingleChildScrollView 没有占用其父级的可用空间。当我删除 SingleChildScrollView 时,布局会正常,但会出现 out of RenderBox 错误。
我应该如何解决这个问题?
【问题讨论】:
-
这行不通,因为你说的广告,SingleChildScrollView 会粘住内容。尝试在列小部件之间使用
SizedBox(height: 10),以腾出空间。或者代替 SingleChildScrollView,使用 ListView
标签: flutter