【问题标题】:Why can't you put a children <Widget>[] in a body: Center widget?为什么不能将子 <Widget>[] 放在 body: Center 小部件中?
【发布时间】:2020-04-20 20:33:54
【问题描述】:

我正在尝试在我的 Flutter 应用程序上创建一个带有浮动操作按钮的列表视图小部件,但它不起作用,因为 Android Studio 不断告诉我:

  • “未定义命名参数children”
  • 我基本上不能把children放在body里:Center widget,但我不知道为什么

我基本上是 Flutter 的初学者,我仍然对基本语法以及哪些小部件可以容纳哪些小部件感到有些困惑,因此非常感谢任何帮助!谢谢!

这是由于第一个错误而无法运行的整体代码(在上面的引号中):

import 'package:flutter/material.dart';

  void main() => runApp(MaterialApp(

    home: Home(),

  ));

  class Home extends StatelessWidget {
    @override
    Widget build(BuildContext context) {

      return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.green[300],
          title: Text(
            'Welcome',
            style: TextStyle(
              fontSize: 25.0,
              fontFamily: 'Raleway',
              letterSpacing: 1.0,
            ),
          ),
          centerTitle: true,
        ),
        body: Center(
            children: <Widget> [
              ListView(
                children: <Widget>[
                  Container(
                    height: 50,
                    color: Colors.green[100],
                    child: Text(
                        'Body Text',
                        style: TextStyle(
                          fontFamily: 'Raleway',
                          fontSize: 45.0,
                          letterSpacing: 1.0,
                          color: Colors.green[300],
                        ),
                      ),
                  ),

                  Container(
                    height: 50,
                    color: Colors.green[100],
                    child: Text(
                      'Text'
                    ),
                  ),
                ],
            ),

            FloatingActionButton(
                onPressed: () {},
                child: Text(
                  '+',
                  style: TextStyle(
                    fontFamily: 'Raleway',
                    fontSize: 35.0,
                  ),
                ),
                backgroundColor: Colors.green[300],
              ),
            ]
        ),
      );

    }
  }

【问题讨论】:

    标签: ios flutter syntax widget


    【解决方案1】:

    这很简单。因为Center 是一个小部件,它只需要一个小部件作为输入。

    它只能对齐作为child 提供的一个小部件。

    对于多个孩子,您必须使用一些将小部件列表作为输入的小部件。 喜欢:

    • 列表视图
    • 包裹 等

    【讨论】:

      【解决方案2】:

      Center 在https://flutter.dev/docs/development/ui/layout 处仅接受一个小部件检查布局页面

      Column(mainAxisAlignment: MainAxisAlignment.center,
           mainAxisSize: MainAxisSize.max,
                  children: <Widget> [
                    ListView(
                      children: <Widget>[
                        Container(
                          height: 50,
                          color: Colors.green[100],
                          child: Text(
                              'Body Text',
                              style: TextStyle(
                                fontFamily: 'Raleway',
                                fontSize: 45.0,
                                letterSpacing: 1.0,
                                color: Colors.green[300],
                              ),
                            ),
                        ),
      
                        Container(
                          height: 50,
                          color: Colors.green[100],
                          child: Text(
                            'Text'
                          ),
                        ),
                      ],
                  ),
      
                  FloatingActionButton(
                      onPressed: () {},
                      child: Text(
                        '+',
                        style: TextStyle(
                          fontFamily: 'Raleway',
                          fontSize: 35.0,
                        ),
                      ),
                      backgroundColor: Colors.green[300],
                    ),
                  ]
              ),
            );
      

      【讨论】:

        【解决方案3】:

        有两种类型的小部件,一种接受一个小部件作为子级,另一种接受 [Widget] 作为子级。

        接受一个小部件作为子小部件:容器小部件、中心小部件、填充小部件等

        接受 [Widget] 作为 Children: Row 小部件、Column 小部件、Stack 小部件、Wrap 小部件、ListView 小部件等

        【讨论】:

          【解决方案4】:

          Center 是一个以自身为中心的小部件。

          所以Center Widget 内只能有 1 个孩子。

          您可以采用类似的结构,

          Center(
           child: Column(
            children: <Widget>[
             All Children you have
            ]
          )
          

          【讨论】:

            【解决方案5】:

            Center Widget 的child 参数的数据类型为Widget,因此它不能将&lt;Widget&gt;[] 作为输入。类似于 int 不接受 String 值。它们是两种不同的数据类型。

            您似乎想要一个位于屏幕中心的数据列表:为此,您可以使用以下代码。

            1.

            Center(
             child: Column(
              mainAxisAlignment: MainAxisAlignment.center, // (optional) will center horizontally.
              children: <Widget>[
               .....
              ]
            )
            

            2.

            Center(
                child: ListView(
                  shrinkWrap:true;
                  children: <Widget>[
                   .....
                  ]
                )
            )
            

            【讨论】:

              猜你喜欢
              • 2019-10-26
              • 2021-09-16
              • 1970-01-01
              • 1970-01-01
              • 2013-05-06
              • 2017-08-27
              • 1970-01-01
              • 1970-01-01
              • 2022-08-04
              相关资源
              最近更新 更多