【问题标题】:How can I change the textbutton color in the flutter showAboutDialog?如何更改颤振 showAboutDialog 中的文本按钮颜色?
【发布时间】:2020-11-11 11:41:22
【问题描述】:

我正在使用 Flutter 中的 showAboutDialog 函数来显示我的项目中使用的许可证。我是如何坚持更改 VIEW LICENSESCLOSE 文本按钮的文本颜色的。请参阅此图片以进行说明:

这是我的代码:

...
onTap: () {
  showAboutDialog(
    context: context,
    applicationName: 'bla',
    applicationLegalese: 'November 2023',
 );
},

到目前为止,我尝试的是在 showAboutDialog 中寻找一个颜色字段,但我却找不到任何东西。我假设我可以更改 MaterialApp ThemeData 的颜色。不幸的是,我找不到特定主题来覆盖这些文本按钮的默认样式。

我在MaterialApp ThemeData 中尝试了以下操作,将VIEW LICENSESCLOSE 的颜色更改为绿色,但这并没有改变任何东西:

textButtonTheme: TextButtonThemeData(style: ButtonStyle(foregroundColor: MaterialStateProperty.all<Color>(Colors.green))

对此有什么想法吗?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    我对这里的答案不满意,因为所有答案都只显示 MaterialColor 用例,而我想要自定义颜色。但我终于在下面的链接中找到了很好的解释。

    https://blog.logrocket.com/new-material-buttons-in-flutter/

    基本上,令人困惑的是新设计使用原色而不是 textStyle 属性。您仍然可以应用其他答案来使用 MaterialColor 更改整体主题,并且您可以使用 TextButton.styleFrom 下的主要颜色使用任何颜色覆盖现有颜色主题。

    应用中任何地方的示例:

    TextButton(
          onPressed: () {},
          style: TextButton.styleFrom(
            primary: Colors.pink,
          ),
          child: Text(
            'TextButton (New)',
            style: TextStyle(fontSize: 30),
          ),
        )
    

    主题示例:

    textButtonTheme: TextButtonThemeData(
          style: TextButton.styleFrom(
            primary: kDarkColor, // This is a custom color variable
            textStyle: GoogleFonts.fredokaOne(),
          ),
        ),
    

    【讨论】:

    • 添加简单的“颜色”键选项会更容易
    【解决方案2】:

    你可以用这个:

    return MaterialApp(
          theme: ThemeData.dark().copyWith(
              textButtonTheme: TextButtonThemeData(
                  style: ButtonStyle(
                      foregroundColor: MaterialStateProperty.resolveWith(
                          (state) => Colors.orange)))),
          home: MyWidget(),
        );
    

    MaterialStateProperty.resolveWith带一个函数,可以根据状态指定颜色,比如

    MaterialState.pressed,
    MaterialState.hovered,
    MaterialState.focused,
    

    More info on this.

    【讨论】:

      【解决方案3】:

      这个怎么样?

      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData(
            primarySwatch: Colors.blue,
            colorScheme: ColorScheme.fromSwatch(
              primarySwatch: Colors.green,
            ).copyWith(),      
          ),
          debugShowCheckedModeBanner: false,
          home: YourScreen(),
        );
      }
      

      【讨论】:

      • 如果我已经有一个 themeData 但我想要一些不同的东西只是为了这个实例/屏幕
      • @lordvidex 我不确定,但是,我认为“showAboutDialog”不会出现这种情况。
      【解决方案4】:

      我运行这段代码。 经过一番研究,我发现了这种改变颜色的方法。

      为此,您需要设置应用程序主主题颜色更改,像这样

        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            theme: ThemeData(
              primarySwatch: Colors.brown,//i am set brown colour,you can set your colour here 
            ),
            debugShowCheckedModeBanner: false,
            home: YourScreen(),
          );
        }
      

      在此之后它的工作,

      showAboutDialog(
                        context: context,
                        applicationName: 'bla',
                        applicationLegalese: 'November 2023',
                      );
      

      【讨论】:

        【解决方案5】:

        如果您只想更改对话框而不是整个应用程序的颜色,则必须创建一个新上下文。用ThemeBuilder 包围显示对话框的按钮

        Theme(
            data: Theme.of(context).copyWith(
              colorScheme: colorScheme.copyWith(primary: Colors.green),
            ),
            child: Builder(
              builder: (context) {
                return ListTile(
                  title: Text('show dialog'),               
                  onTap: () => showAboutDialog(
                                  context: context,
                                  ...) 
                );
              },
            ),
          )
        

        【讨论】:

          猜你喜欢
          • 2021-11-24
          • 1970-01-01
          • 2021-12-14
          • 2021-06-07
          • 1970-01-01
          • 2021-11-12
          • 1970-01-01
          • 2012-06-25
          • 1970-01-01
          相关资源
          最近更新 更多