首页 > 解决方案 > 我如何表示行中的两个文本字段

问题描述

所以我正在尝试创建这样的布局。我在水平方向有 2 个文本字段,但将其包装成一行。但是在重新加载后没有文本字段出现:

在此处输入图像描述

但是,通过此代码,在重新加载后,这些都不会出现:

Column(
                    children: <Widget>[
                      SizedBox(height: 10.0),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          TextField(
                            decoration: InputDecoration(
                                labelText: 'Name',
                                labelStyle: TextStyle(
                                    color: Colors.grey[400]
                                )
                            ),
                          ),
                          SizedBox(width: 10.0),
                          TextField(
                            decoration: InputDecoration(
                                labelText: 'Name',
                                labelStyle: TextStyle(
                                    color: Colors.grey[400]
                                )
                            ),
                          ),
                        ],
                      ),
                      SizedBox(height: 10.0),
                      TextField(
                        decoration: InputDecoration(
                            labelText: 'Email/Mobile Number',
                            labelStyle: TextStyle(
                              color: Colors.grey[400],
                            )
                        ),
                      ),
                      SizedBox(height: 10.0),
                      TextField(
                        decoration: InputDecoration(
                            labelText: 'Password',
                            labelStyle: TextStyle(
                              color: Colors.grey[400],
                            )
                        ),
                      ),
                    ],
                  )

标签: android-studioflutterdartflutter-layoutflutter-dependencies

解决方案


你必须使用一个Expanded小部件。将您的Row小部件替换为以下小部件:

  Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Expanded(
                              // optional flex property if flex is 1 because the default flex is 1
                              flex: 1,
                              child: TextField(
                                decoration: InputDecoration(
                                    labelText: 'Name',
                                    labelStyle: TextStyle(
                                        color: Colors.grey[400]
                                    )
                                ),
                              ),
                            ),
                            SizedBox(width: 10.0),
                            Expanded(
                              // optional flex property if flex is 1 because the default flex is 1
                              flex: 1,
                              child: TextField(
                                decoration: InputDecoration(
                                    labelText: 'Name',
                                    labelStyle: TextStyle(
                                        color: Colors.grey[400]
                                    )
                                ),
                              ),
                            ),
                          ],
                        ),


推荐阅读