【问题标题】:Is there a way to include Multiple Children inside a Container?有没有办法在一个容器中包含多个孩子?
【发布时间】:2019-12-24 12:01:35
【问题描述】:

这是完整的代码:

class Application extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
       body: new Container(
         color: Color(0xff258DED),
         height: 400.0,
         alignment: Alignment.center,
         child: new Container(
           height: 200.0,
           width: 200.0,
           decoration: new BoxDecoration(
             image: DecorationImage(
                 image: new AssetImage('assets/logo.png'),
             fit: BoxFit.fill
             ),
             shape: BoxShape.circle
           ),
         ),
         child: new Container(
          child: new Text('Welcome to Prime Message',
            textAlign: TextAlign.center,
            style: TextStyle(
                fontFamily: 'Aleo',
                fontStyle: FontStyle.normal,
                fontWeight: FontWeight.bold,
                fontSize: 25.0,
                color: Colors.white
            ),
          ),
         ),
        ),
      ),
    );
  }
}

我尝试在顶部添加Container,然后在其中添加了两个孩子。第一个孩子工作正常,但第二个孩子给了我错误,例如“已指定命名参数'child'的参数”。

【问题讨论】:

  • 您必须使用多子小部件,例如 Column 、 Row 、 Stack ...。它加深了您想要放置小部件的方式

标签: flutter dart flutter-layout


【解决方案1】:

如果您尝试将多个孩子放入容器中,您希望将线性孩子视为Row() 或Column() 类。 Stack() 用于如果你想让浮动的孩子在彼此之上。

例如:

class Application extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return new MaterialApp(
            home: new Scaffold(
                body: new Container(
                    color: Color(0xff258DED),
                    height: 400.0,
                    alignment: Alignment.center,
                    child: new Column(
                        children: [
                            new Container(
                                height: 200.0,
                                width: 200.0,
                                decoration: new BoxDecoration(
                                    image: DecorationImage(
                                        image: new AssetImage('assets/logo.png'),
                                        fit: BoxFit.fill
                                    ),
                                    shape: BoxShape.circle
                                ),
                            ),
                            new Container(
                                child: new Text('Welcome to Prime Message',
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                        fontFamily: 'Aleo',
                                        fontStyle: FontStyle.normal,
                                        fontWeight: FontWeight.bold,
                                        fontSize: 25.0,
                                        color: Colors.white
                                    ),
                                ),
                            ),
                        ],
                    ),
                ),
            ),
        );
    }
}

【讨论】:

  • 谢谢我看看
【解决方案2】:

在容器中可以是唯一的孩子。 如果您希望它们垂直放置,则必须使用 Column 小部件并将 Container 小部件添加为子级。

 @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        body: new Container(
          color: Color(0xff258DED),
          height: 400.0,
          alignment: Alignment.center,
          child: Column(
            children: <Widget>[
              Container(
                height: 200.0,
                width: 200.0,
                decoration: new BoxDecoration(
                    image: DecorationImage(
                        image: new AssetImage('assets/logo.png'),
                        fit: BoxFit.fill
                    ),
                    shape: BoxShape.circle
                ),
              ),
              Container(
                child:Text('Welcome to Prime Message',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      fontFamily: 'Aleo',
                      fontStyle: FontStyle.normal,
                      fontWeight: FontWeight.bold,
                      fontSize: 25.0,
                      color: Colors.white
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

提示 #1 您不必使用“新”词来创建对象。 提示 #2 在 flutter.dev 上阅读有关列、行和堆栈的信息

【讨论】:

    【解决方案3】:

    请检查以下代码:-

    @override
      Widget build(BuildContext mContext) {
        return MaterialApp(
          home: Scaffold(
            body: Container(
              color: Color(0xff258DED),
              height: 400.0,
              alignment: Alignment.center,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    height: 200.0,
                    width: 200.0,
                    decoration: BoxDecoration(
                        image: DecorationImage(
                            image: AssetImage('assets/logo.png'),
                            fit: BoxFit.fill
                        ),
                        shape: BoxShape.circle
                    ),
                  ),
                  Container(
                    child: Text('Welcome to Prime Message',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontFamily: 'Aleo',
                          fontStyle: FontStyle.normal,
                          fontWeight: FontWeight.bold,
                          fontSize: 25.0,
                          color: Colors.white
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    

    【讨论】:

      猜你喜欢
      • 2022-07-18
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 2020-08-15
      相关资源
      最近更新 更多