【问题标题】:The argument type 'MaterialColor' can't be assigned to the parameter type 'MaterialStateProperty<Color>参数类型“MaterialColor”不能分配给参数类型“MaterialStateProperty<Color>
【发布时间】:2021-08-21 02:55:43
【问题描述】:

我有一个与新的 ElevatedButtonThemeData 小部件相关的问题,基本上我想为我的应用程序中的所有 ElevatedButtons 设置背景颜色,我正在努力尝试通过以下方式在 ThemeData 定义中设置它:

      theme: ThemeData(
        ...
        elevatedButtonTheme: ElevatedButtonThemeData(
            style: ButtonStyle(backgroundColor: Colors.red)), // Here Im having the error
        ...
        ),
      ),

错误:

参数类型“MaterialColor”不能分配给参数类型“MaterialStateProperty?'.dartargument_type_not_assignable)

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    阅读文档后,我找到了设置颜色的方法。

      theme: ThemeData(
        ...
        elevatedButtonTheme: ElevatedButtonThemeData(
            style: ButtonStyle(backgroundColor: MaterialStateProperty.all<Color>(Colors.red))), // Here Im having the error
        ...
        ),
      ),
    

    【讨论】:

    • 这很完美!
    【解决方案2】:

    这是一个代码 sn-p,它显示了我如何使用材质状态属性设置文本按钮的样式。

    TextButton(style: ButtonStyle(
          padding: MaterialStateProperty.all(const EdgeInsets.all(0)),
          elevation: MaterialStateProperty.all(8),
          shape: MaterialStateProperty.all(
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(50))),
          backgroundColor: MaterialStateProperty.all(Colors.transparent),
          shadowColor: MaterialStateProperty.all(
              Theme.of(context).colorScheme.onSurface),
        ),),
    

    我希望这能给你一个想法。

    【讨论】:

      猜你喜欢
      • 2022-09-26
      • 2018-12-26
      • 2021-08-27
      • 2021-01-09
      • 2022-12-03
      • 2022-12-22
      • 2021-08-22
      • 2021-01-05
      • 2023-02-07
      相关资源
      最近更新 更多