首页 > 解决方案 > 强制列中的小部件匹配最大的

问题描述

我在一列中有几个 RaisedButton 小部件,它们根据它们的文本具有不同的宽度。我希望它们具有相同的宽度,但我不想设置静态宽度,因为它应该能够响应语言和屏幕尺寸/形状的变化。较小的小部件应调整大小以匹配列中最大小部件的宽度。这样做的最佳方法是什么?

谢谢!

Column(
                children: <Widget>[
                  RaisedButton(
                      child: Text("Btn1"),
                      onPressed: (){},
                  ),
                  RaisedButton(
                      child: Text("LongerBtn2"),
                      color: Colors.Red,
                      onPressed: (){},
                  )
                ],
              ),

标签: flutterdart

解决方案


可以这样做:

        IntrinsicWidth( // <- Sizes its child's width to the child's maximum intrinsic width
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch, // <- all children will try to take as much space as possible 
            children: <Widget>[
              RaisedButton(
                child: Text("Btfffffff fsdfds dsfsa n1"),
                color: Colors.green,
                onPressed: () {},
              ),
              RaisedButton(
                child: Text("LongerBtn2"),
                color: Colors.red,
                onPressed: () {},
              )
            ],
          ),
        )

推荐阅读