【问题标题】:Why can I not use children array here? Flutter为什么我不能在这里使用儿童数组?扑
【发布时间】:2021-01-24 21:53:26
【问题描述】:

我有这门课,从其中一个例子中学习和改变

class SignUpView extends StatelessWidget {
  const SignUpView({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SizedBox(
          width: 400,
          child: Card(
            child: SignUpForm(),
          ),
        ),
      ),
    );
  }
}

但是如果我想放 children ,而不是 child,像这样

class SignUpView extends StatelessWidget {
  const SignUpView({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        children: [
          SizedBox(
            width: 400,
            child: Card(
              child: SignUpForm(),
            ),
          ),
        ],
      ),
    );
  }
}

它说未定义命名参数children。

如果我想在 Center 容器中放置多个孩子怎么办?

【问题讨论】:

    标签: flutter layout flutter-layout


    【解决方案1】:

    Center 只能有一个孩子。使用 ColumnRowListView 之类的东西为 Center 使用多个小部件。

    import 'package:flutter/material.dart';
    
    class SignUpView extends StatelessWidget {
      const SignUpView({Key key}) : super(key: key);
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: ListView(
              children: [
                SizedBox(
                  width: 400,
                  child: Card(
                    child: SignUpForm(),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 很高兴能为您提供帮助。 Flutter 社区很棒:D
    • 是的,我也这么认为
    • 有没有一个地方可以让我看到一个小部件对于所有小部件可以有多少个子级,以及可以在什么中嵌套什么?
    猜你喜欢
    • 2021-03-11
    • 2013-02-12
    • 2021-01-06
    • 2023-03-29
    • 2021-02-16
    • 2018-06-19
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多