对齐元素独立于颤动的行/列

时间:2020-05-03 17:31:32

标签: flutter flutter-layout

我是个陌生人,最近对代码进行了宽泛的处理,我遇到了一种情况,即我必须不合理地对齐行中的元素。我有3个子元素,分别称为A,B和C。我的要求是,将A和B放在行的最左边,将元素C放在最右边。

期望: enter image description here

我的代码:

Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              CircleAvatar(
                backgroundImage: NetworkImage(''),
                radius: 30,
              ),
              SizedBox(
                width: 10,
              ),
              Flexible(
                child: Text('name', overflow: TextOverflow.ellipsis,),
              ),
              SizedBox(
                width: 10,
              ),
              Container(
                child: ButtonTheme(
                  height: 25.0,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(100.0)),
                  child: RaisedButton(
                    child: Text(
                      'Challenge',
                      style: TextStyle(color: Colors.white, fontSize: 12),
                    ),
                    onPressed: () {},
                  ),
                ),
              ),
            ],
          ),

但是输出如下: enter image description here

当我尝试使用Spacer()时,输出如下: enter image description here

任何人都可以帮助我。预先感谢。

3 个答案:

答案 0 :(得分:1)

您必须将Text小部件包装在Expanded小部件中,如下例所示。 试试这个,效果很好, 检查下面的代码:


 Row(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                CircleAvatar(
                  backgroundImage: NetworkImage(''),
                  radius: 30,
                ),
                SizedBox(
                  width: 10,
                ),
                Expanded(
                 // wrap your text widget with an Expanded widget
                  child: Text(
                    'name',
                    overflow: TextOverflow.ellipsis,
                    textAlign: TextAlign.start,
                  ),
                ),
                SizedBox(
                  width: 10,
                ),
                Container(
                  child: ButtonTheme(
                    height: 25.0,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(100.0)),
                    child: RaisedButton(
                      child: Text(
                        'Challenge',
                        style: TextStyle(color: Colors.white, fontSize: 12),
                      ),
                      onPressed: () {},
                    ),
                  ),
                ),
              ],
            ),

希望对您有帮助。

答案 1 :(得分:1)

使用扩展小部件包裹文本,并将文本小部件的textAlign设置为TextAlign.start,如下代码:

 Row(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                CircleAvatar(
                  backgroundImage: NetworkImage(''),
                  radius: 30,
                ),
                SizedBox(
                  width: 10,
                ),
                Expanded(
                  child: Text(
                    'name',
                    overflow: TextOverflow.ellipsis,
                    textAlign: TextAlign.start,
                  ),
                ),
                SizedBox(
                  width: 10,
                ),
                Container(
                  child: ButtonTheme(
                    height: 25.0,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(100.0)),
                    child: RaisedButton(
                      child: Text(
                        'Challenge',
                        style: TextStyle(color: Colors.white, fontSize: 12),
                      ),
                      onPressed: () {},
                    ),
                  ),
                ),
              ],
            ),

答案 2 :(得分:1)

尝试一下,它应该对您有用。

Row(
  mainAxisSize: MainAxisSize.min,
  children: <Widget>[
      CircleAvatar(
         backgroundImage: NetworkImage(''),
         radius: 30,
      ),
      SizedBox(width: 10),
      Expanded(
        child: Text('name', align: TextAlign.start, overflow: TextOverflow.ellipsis),
      ),
      SizedBox(width: 10),
      Container(
        child: ButtonTheme(
           height: 25.0,
           shape: RoundedRectangleBorder(
               borderRadius: BorderRadius.circular(100.0)),
               child: RaisedButton(
                  child: Text('Challenge', style: TextStyle(color: Colors.white, fontSize: 12)),
                  onPressed: () {},
               )
           )
        )
  ]
)