【问题标题】:The column's MainAxisAlignment does not work inside a SingleChildScollView列的 MainAxisAlignment 在 SingleChildScollView 内不起作用
【发布时间】: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


【解决方案1】:

试试下面的代码希望它对你有帮助。使用 CustomScrollView 而不是 SingleChildScrollView 只需更改您的文本字段而不是我的文本字段。您还使用了SizedBox(height:20,), 也参考 CustomScrollView here

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))),
        child: CustomScrollView(
          scrollDirection: Axis.vertical,
          slivers: [
            SliverFillRemaining(
              child: Form(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Column(
                      children: [
                        const Text('Login',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 24)),
                        TextField(),
                        TextField(), // Row of TextFormField and IconButton
                      ],
                    ),
                    ElevatedButton(
                      onPressed: () {},
                      child: Text('Submit'),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    ),
    Expanded(
      flex: 1,
      child: Container(),
    )
  ],
);

您的结果屏幕->

【讨论】:

  • 它解决了RenderBox问题吗?
  • 你在哪里出现这个问题?
  • 如果您构建桌面应用并手动最小化屏幕高度,则会发生渲染框溢出。
猜你喜欢
  • 2022-01-18
  • 2023-02-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 2021-09-17
  • 2021-04-05
  • 2021-03-23
  • 2019-01-12
相关资源
最近更新 更多