【问题标题】:How can I use both ButtonStyle() and ElevatedButton.styleFrom() in flutter?如何在 flutter 中同时使用 ButtonStyle() 和 ElevatedButton.styleFrom() ?
【发布时间】:2023-01-25 16:05:05
【问题描述】:

嗨,我是 Flutter 的新手,我想将禁用的颜色和全屏宽度应用于 ElevatedButton。

所以为了应用颜色,我这样做了:

ElevatedButton(
  style : ButtonStyle(
    backgroundColor : MaterialStateProperty.resolveWith<Color>(
      (Set<MaterialState> states) {
        if (states.contains(MaterialState.disabled)) {return Colors.green;}
        else {return Colors.blue;}
      }
    ),
    ...

为了应用宽度,我这样做了:

ElevatedButton(
  style : ElevatedButton.styleFrom(
    minimumSize : const Size.fromHeight(50)
  ),
  ...

但我不知道如何将它们结合起来。请告诉我。

谢谢,

【问题讨论】:

    标签: flutter


    【解决方案1】:

    你也可以在ButtonStyle中设置minimumSize,像这样:

    ElevatedButton(
      style: ButtonStyle(
        minimumSize: MaterialStateProperty.all(Size.fromHeight(50)), /// <--- add this
        backgroundColor: MaterialStateProperty.resolveWith<Color>(
            (Set<MaterialState> states) {
          if (states.contains(MaterialState.disabled)) {
            return Colors.green;
          } else {
            return Colors.blue;
          }
        }),
      ),
      onPressed: (){},
      child: Container(),
    )
    

    【讨论】:

      猜你喜欢
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 2023-02-04
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多