简体   繁体   English

如何更改颤动 showAboutDialog 中的文本按钮颜色?

[英]How can I change the textbutton color in the flutter showAboutDialog?

I'm using the showAboutDialog function from flutter to show used licences in my project.我正在使用showAboutDialog函数来显示我的项目中使用的许可证。 How ever I'm stuck with changing the text color of the VIEW LICENSES and CLOSE textbuttons.我是如何坚持更改VIEW LICENSESCLOSE textbuttons 的文本颜色的。 See this image for clarification:请参阅此图像以进行澄清:

关于对话框

This is my code:这是我的代码:

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

What I tried so far is looking for a color field inside the showAboutDialog how ever I could not find anything.到目前为止,我尝试在showAboutDialog寻找颜色字段,但我找不到任何东西。 I'm assuming that I could change the color in my MaterialApp ThemeData .我假设我可以更改MaterialApp ThemeData的颜色。 Unfortunately I was not able to find the specific theme to override the default styling of those textbuttons.不幸的是,我无法找到特定主题来覆盖这些文本按钮的默认样式。

I tried the following in my MaterialApp ThemeData to change the color of VIEW LICENSES and CLOSE to green but that did not change anything:我在我的MaterialApp ThemeData尝试了以下ThemeData来将VIEW LICENSESCLOSE的颜色更改为绿色,但这并没有改变任何东西:

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

Any ideas about this?关于这个的任何想法?

I was not satisfied with the answers here because all were showing only MaterialColor use-cases and I wanted a custom color.我对这里的答案并不满意,因为所有答案都只显示了 MaterialColor 用例,而我想要自定义颜色。 But I finally found something explaining it well on the following link.但我终于在以下链接上找到了一些解释得很好的东西。

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

Basically, what is confusing is that the new design uses the primary color instead of the textStyle property.基本上,令人困惑的是新设计使用原色而不是 textStyle 属性。 You can still apply the other answers to change the overall theme using a MaterialColor, and you can override the existing color theme using any color by using primary under TextButton.styleFrom.您仍然可以应用其他答案来使用 MaterialColor 更改整体主题,并且您可以通过使用 TextButton.styleFrom 下的主要颜色使用任何颜色覆盖现有颜色主题。

Example for anywhere in the app:应用程序中任何位置的示例:

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

Example for the theme:主题示例:

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

You can use this:你可以使用这个:

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

MaterialStateProperty.resolveWith takes a function, you can specify the color based on states, such as MaterialStateProperty.resolveWith带一个函数,可以根据状态指定颜色,比如

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

More info on this . 更多信息

How about this one?这个怎么样?

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

样本

i run this code.我运行这个代码。 after some research i find out this way to change colour.经过一些研究,我发现了这种改变颜色的方法。

for this you need to set application main theme colour change, like this为此,您需要设置应用程序主主题颜色更改,如下所示

  @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(),
    );
  }

after this its work,在这之后它的工作,

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

在此处输入图片说明

If you want to change the colors only for the dialog and not for the whole app, you have to create a new context.如果您只想更改对话框的颜色而不是整个应用程序的颜色,则必须创建一个新的上下文。 Surround the Button that showing the dialog with a Theme and a BuilderThemeBuilder包围显示对话框的按钮

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,
                          ...) 
        );
      },
    ),
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM