简体   繁体   English

Flutter 在显示/隐藏小部件上显示错误

[英]Flutter showing error on show/hide widget

Flutter i just need to hide my component on button pressed so i have set the boolean value and use if condition but problem is its showing error Columns children must not contain null value. Flutter 我只需要在按下按钮时隐藏我的组件,所以我设置了 boolean 值并在条件下使用,但问题是其显示错误列子项不得包含 null 值。

Here is my code这是我的代码

 Widget build(BuildContext context) {
    var first = true;
    var second = false;
    Size size = MediaQuery.of(context).size;
    return Background(
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "SIGNUP",
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            SizedBox(height: size.height * 0.03),
            SvgPicture.asset(
              "assets/icons/signup.svg",
              height: size.height * 0.35,
            ),
            first ? RoundedInputField(
              hintText: "Your Name",
              icon: Icons.person,
              onChanged: (value) {},
            ) : null ,
            first ? RoundedInputField(
              hintText: "Your Email",
              icon: Icons.mail,
              onChanged: (value) {
                print(value);
              },
            ) : null ,

            first ? RoundedPasswordField(
              onChanged: (value) {},
            ) : null ,

            second ? RoundedInputField(
              hintText: "Your Number",
              icon: Icons.phone,
              onChanged: (value) {
                print(value);
              },
            ) : null ,

            first ? RoundedButton(
              text: "NEXT",
              press: () {
                first = false;
                second = true;
              },
            ) : null ,
            second ? RoundedButton(
              text: "REGISTER",
              press: () {

              },
            ) : null ,
            SizedBox(height: size.height * 0.03),
            AlreadyHaveAnAccountCheck(
              login: false,
              press: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) {
                      return LoginScreen();
                    },
                  ),
                );
              },
            ),

          ],
        ),
      ),
    );
  }
}

I am just hiding my input widget on click and after click need to show other input and button.我只是在点击时隐藏我的输入小部件,点击后需要显示其他输入和按钮。 But getting error Columns children must not contain any null values但得到错误列子项不得包含任何 null 值

Use a SizedBox() or an empty Container() instead of null使用SizedBox()或空Container()代替null

Widget build(BuildContext context) {
    var first = true;
    var second = false;
    Size size = MediaQuery.of(context).size;
    return Background(
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "SIGNUP",
              style: TextStyle(fontWeight: FontWeight.bold),
            ),
            SizedBox(height: size.height * 0.03),
            SvgPicture.asset(
              "assets/icons/signup.svg",
              height: size.height * 0.35,
            ),
            first ? RoundedInputField(
              hintText: "Your Name",
              icon: Icons.person,
              onChanged: (value) {},
            ) : SizedBox() ,
            first ? RoundedInputField(
              hintText: "Your Email",
              icon: Icons.mail,
              onChanged: (value) {
                print(value);
              },
            ) : SizedBox() ,

            first ? RoundedPasswordField(
              onChanged: (value) {},
            ) : SizedBox() ,

            second ? RoundedInputField(
              hintText: "Your Number",
              icon: Icons.phone,
              onChanged: (value) {
                print(value);
              },
            ) : SizedBox() ,

            first ? RoundedButton(
              text: "NEXT",
              press: () {
                first = false;
                second = true;
              },
            ) : SizedBox() ,
            second ? RoundedButton(
              text: "REGISTER",
              press: () {

              },
            ) : SizedBox() ,
            SizedBox(height: size.height * 0.03),
            AlreadyHaveAnAccountCheck(
              login: false,
              press: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) {
                      return LoginScreen();
                    },
                  ),
                );
              },
            ),

          ],
        ),
      ),
    );
  }
}

You need to return Container() if the first & second variable is null Column widget does not know how to render null you must tell Column widget to render empty content if it's null.如果第一个和第二个变量是 null,则需要返回 Container()

Eg:例如:

first ? RoundedButton(
              text: "NEXT",
              press: () {
                first = false;
                second = true;
              },
            ) : Container(),
            second ? RoundedButton(
              text: "REGISTER",
              press: () {

              },
            ) : Container(),

Flutter does not accepts null values as a Widget. Flutter 不接受null值作为小部件。 You must provide at least an empty Widget if don't want to show nothing inside another widget.如果不想在另一个小部件中显示任何内容,您必须至少提供一个空的小部件。 Usually you can use Container(width: 0, height:0) or even Text('') .通常你可以使用Container(width: 0, height:0)甚至Text('')

All the answers are valid, you can however use Visibility to show or hide a widget, it looks like this所有答案都是有效的,但是您可以使用Visibility来显示或隐藏小部件,它看起来像这样

Visibility(
 visible: YOUR_BOOLEAN_VALUE,
 child: YOUR_WIDGET,
)

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

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