简体   繁体   English

如何将 ListView 放在带有填充的脚手架下?

[英]How do I put a ListView under a scaffold with padding?

I am quite new to flutter and I've been trying to set up a profile page for my app but it seems like I don't quite understand how these flutter widgets come together, I apologize if my question is quite dumb but I've been googling and failing at getting this to work.我对 flutter 很陌生,我一直在尝试为我的应用程序设置个人资料页面,但似乎我不太明白这些 flutter 小部件是如何组合在一起的,如果我的问题很愚蠢,我很抱歉,但我已经一直在谷歌搜索并未能使其正常工作。 So basically I have a scaffold which holds a container thats supposed to display profile info (email, name, etc.), under it I'd like to place a listview, but flutter has been boggling my mind, I can't seem to understand how layouts work together, here is my code.所以基本上我有一个脚手架,里面有一个应该显示个人资料信息(电子邮件、姓名等)的容器,在它下面我想放置一个列表视图,但是 flutter 一直让我难以置信,我似乎不能了解布局如何协同工作,这是我的代码。 When I try to do buildPage(), I get an error that the scaffold has infinite size, using buildBox() alone works.当我尝试执行 buildPage() 时,我收到一个错误,即脚手架具有无限大小,单独使用 buildBox() 可以工作。 I'm not sure how to go about this.我不确定如何 go 关于这个。 Any Help is appreciated任何帮助表示赞赏

    import 'package:flutter/material.dart';

    class ProfileBox extends StatefulWidget {
      final String userEmail;
      const ProfileBox(this.userEmail);

      @override
      _ProfileBoxState createState() => _ProfileBoxState();
    }

    class _ProfileBoxState extends State<ProfileBox> {


      @override
      Widget build(BuildContext context) {
       return _buildPage();
      }



      Widget _buildBox(){
     return Scaffold(
          body: Align(
            alignment: Alignment.topCenter,
            child: LayoutBuilder(
              builder: (BuildContext context, BoxConstraints constraints) {
                return Container(
                      margin: const EdgeInsets.only(top: 20.0),
                      decoration: BoxDecoration(
                      color: Color(0xFF8185E2), border: Border.all(
                      color: Color(0xFF8185E2),
                      ),
                      borderRadius: BorderRadius.all(Radius.circular(20))
                      ),
                  height: constraints.maxHeight / 2.5,
                  width: MediaQuery.of(context).size.width -  (MediaQuery.of(context).size.width * 5)/100,
                   child: Center(
            child: Text(
             widget.userEmail,
              textAlign: TextAlign.center,
            ),
          ),
                );
              },
            ),
          ),
        );
      }


      Widget _buildPage()
      {
        return Column(children: <Widget>[
          _buildBox(),
          _buildList(),

        ],);

      }

      Widget _buildList()
      {
         return ListView(
            children: <Widget>[
              ListTile(
                title: Text('Sun'),
              ),
              ListTile(
                title: Text('Moon'),
              ),
              ListTile(
                title: Text('Star'),
              ),
            ],
          );
      }

}

Scaffold Widget should be a top Widget that contains the Column widget and all children Widget. Scaffold Widget 应该是一个包含Column Widget 和所有子 Widget 的顶级 Widget。 I think you can start learning how to layout Widget in Flutter in order to understand more the way how Widget works, and the good place can be:https://flutter.dev/docs/development/ui/layout#lay-out-a-widget我想你可以开始学习如何在 Flutter 中布局 Widget,以便更多地了解 Widget 的工作方式,好地方可以是:https://flutter.dev/docs/development/ui/layout#lay-out-一个小部件

Coming back to your question, you can just fix a bit to make it work:回到您的问题,您只需稍作修改即可使其正常工作:

class _ProfileBoxState extends State<ProfileBox> {
  @override
  Widget build(BuildContext context) {
    return _buildPage();
  }

  Widget _buildBox() {
    return Align(
      alignment: Alignment.topCenter,
      child: LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          return Container(
            margin: const EdgeInsets.only(top: 20.0),
            decoration: BoxDecoration(
                color: Color(0xFF8185E2),
                border: Border.all(
                  color: Color(0xFF8185E2),
                ),
                borderRadius: BorderRadius.all(Radius.circular(20))),
            height: constraints.maxHeight / 2.5,
            width: MediaQuery.of(context).size.width -
                (MediaQuery.of(context).size.width * 5) / 100,
            child: Center(
              child: Text(
                widget.userEmail,
                textAlign: TextAlign.center,
              ),
            ),
          );
        },
      ),
    );
  }

  Widget _buildPage() {
    return Scaffold(
      body: Column(
        children: <Widget>[
          _buildBox(),
          _buildList(),
        ],
      ),
    );
  }

  Widget _buildList() {
    return ListView(
      children: <Widget>[
        ListTile(
          title: Text('Sun'),
        ),
        ListTile(
          title: Text('Moon'),
        ),
        ListTile(
          title: Text('Star'),
        ),
      ],
    );
  }
}

I just modified your code with Scaffold put the top Widget before the SafeArea , Please check the below code of it.我刚刚用Scaffold修改了您的代码,将顶部 Widget 放在SafeArea之前,请检查下面的代码。

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class HomeScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _HomeScreen();
  }
}

class _HomeScreen extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return _buildPage();
  }

  Widget _buildPage() {
    return SafeArea(
      top: true,
      child: Scaffold(
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
              margin: const EdgeInsets.only(top: 20.0),
              decoration: BoxDecoration(
                  color: Color(0xFF8185E2),
                  border: Border.all(
                    color: Color(0xFF8185E2),
                  ),
                  borderRadius: BorderRadius.all(Radius.circular(20))),
              height: MediaQuery.of(context).size.height / 2.5,
              width: MediaQuery.of(context).size.width -
                  (MediaQuery.of(context).size.width * 5) / 100,
              child: Center(
                child: Text(
                  "YOUR_EMAIL",
                  textAlign: TextAlign.center,
                ),
              ),
            ),
            Expanded(
              child: _buildList(),
            )
          ],
        ),
      ),
    );

    Column(
      children: <Widget>[
        //  _buildBox(),
        _buildList(),
      ],
    );
  }

  Widget _buildList() {
    return ListView(
      children: <Widget>[
        ListTile(
          title: Text('Sun'),
        ),
        ListTile(
          title: Text('Moon'),
        ),
        ListTile(
          title: Text('Star'),
        ),
      ],
    );
  }
}

And output of the program as follow和output的程序如下

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM