【问题标题】:Building grid layouts with Flutter使用 Flutter 构建网格布局
【发布时间】:2020-08-09 05:00:17
【问题描述】:

我是 Flutter 的新手,对如何使用小部件来创建原生 Android、RelativeLayout 和 ConstraintLayout 的可持续替代品感到困惑,但无法正确使用 Column、Row 小部件。我正在尝试构建此布局,但直到现在都无法构建。

我首先为整个事物添加一个容器,然后为每个“块”添加一个列,然后对其余部分使用行/容器。这是我到目前为止编写的代码,但这并没有显示任何内容:

Container(
            height: 250,
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 24),
              child: Column(
                children: <Widget>[
                  Container(
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                      gradient: LinearGradient(
                        colors: colorItems // a color list at global scope with 2 items
                      ),
                      color: Colors.black
                    ),
                  )
                ],
              ),
            ),
          )

编辑

请像这样解释您的小部件层次结构:

Container
  - Column
    - Row

【问题讨论】:

    标签: flutter dart flutter-layout hybrid-mobile-app


    【解决方案1】:

    我添加了一个完整的例子来说明你想要得到什么:

    以下是小部件树的外观:

    WIDGET HEIRARCHY
    Container
      - Column
        - Container
        - SizedBox
        - Expanded
          -Row
            - Container
            - SizedBox
            -Expanded
              - Column
                - Container
                - SizedBox
                - Expanded
                  - Row
                    - Expanded
                      - Container
                    - Expanded
                      - Container
                    - Expanded
                      - Container
    

    代码

    class StackOver extends StatelessWidget {
      final BoxDecoration _boxDecoration = BoxDecoration(
        color: Colors.blue,
        borderRadius: BorderRadius.circular(
          15.0,
        ),
      );
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(
              'Tab bar',
            ),
          ),
          body: Padding(
            padding: const EdgeInsets.all(10.0),
            // parent container housing all other widgets
            child: Container(
              height: 250,
              child: Column(
                children: [
                  // frist container [give it a height, it takes up the width of the parent]
                  Container(
                    height: 80,
                    decoration: _boxDecoration,
                  ),
    
                  // add spacing
                  SizedBox(
                    height: 15,
                  ),
    
                  // second child of the column [consists of a Row with children]
                  Expanded(
                    child: Row( // row 
                      children: [
                        Container( // first child 
                          width: 80, // give it a width, it takes up the height of parent since it is wrapped with an expanded widget
                          decoration: _boxDecoration,
                        ),
    
                        // add spacing
                        SizedBox( // second child
                          width: 15,
                        ),
    
    
                        Expanded( // thrid child [consists a column with children ]
                          child: Column(
                            children: [
                              Container( 
                                height: 80, // give it a height, it takes up the width of parent since it is wrapped with an expanded widget
                                decoration: _boxDecoration,
                              ),
    
                              // add spacing
                              SizedBox( // second child
                                height: 20,
                              ),
    
    
                              Expanded( // third child [consists of a row with 3 containsers]
                                child: Row( 
                                  children: [
                                    Expanded( // first container
                                      child: Container(
                                        decoration: _boxDecoration,
                                      ),
                                    ),
    
                                    // add spacing
                                    SizedBox(
                                      width: 15,
                                    ),
                                    Expanded( // second container
                                      child: Container(
                                        decoration: _boxDecoration,
                                      ),
                                    ),
    
                                    // add spacing
                                    SizedBox(
                                      width: 15,
                                    ),
                                    Expanded( // third container
                                      child: Container(
                                        decoration: _boxDecoration,
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    
    

    输出

    【讨论】:

    • 非常感谢!我马上试试。另外,如果您不介意,您可以添加您使用的小部件的层次结构吗?就像我在问题中编辑过的一样
    • 我添加了小部件层次结构,您可以查看。 @MoodBoard
    猜你喜欢
    • 2017-10-26
    • 2019-08-19
    • 2020-08-31
    • 1970-01-01
    • 2012-06-04
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    相关资源
    最近更新 更多