在 Flutter 中居中 ListView

Centering ListView in Flutter

我正在尝试制作带有徽标的登录/注册屏幕。我需要它们具有响应性,因此可以适合大多数移动屏幕。为此,我使用了 ListView。但是,我只需要将 ListView 居中放置在我的布局中。有什么建议吗?

这是我的尝试:

@override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
        child: ListView(
          children: <Widget>[
            Image.asset('assets/images/logo.png', scale: 3.0,),
            InputField('enter email address', Icons.email, TextInputType.emailAddress),
            PasswordInputField('enter password', Icons.lock, TextInputType.text),
            RoundBtn('SIGN IN', signIn),
            RoundBtn('SIGN UP', () => {}),
            OutlineBtn('FORGOT PASSWORD?', () => {})
          ],
        ),
      )
    );
  }

Login Screen Registration Screen

使用 SingleChildScrollView 而不是 ListView。 试试这个...

 @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
          child: SingleChildScrollView(
            child: Column(
            children: <Widget>[
              Image.asset('assets/images/logo.png', scale: 3.0,),
              InputField('enter email address', Icons.email, TextInputType.emailAddress),
              PasswordInputField('enter password', Icons.lock, TextInputType.text),
              RoundBtn('SIGN IN', signIn),
              RoundBtn('SIGN UP', () => {}),
              OutlineBtn('FORGOT PASSWORD?', () => {})
            ],
          ),
        ),)
    );
  }

ListView 无法添加到屏幕中央。尽管如此,仍然没有官方文档。但是,如果你想做

示例:

Center(
    child: ListView(
      shrinkWrap: true,
      children: <Widget>[
        Center(child: Text('Text1')),
        Center(child: Text('Text2')),
        Center(child: Text('Text3')),
      ],
    ),
  )

输出:

  • 在 ListView 中使用 shrinkWrap: true
 child: ListView(
      shrinkWrap: true,
      ......
    ),