[英]How can I change the textbutton color in the flutter showAboutDialog?
我正在使用showAboutDialog
函数来显示我的项目中使用的许可证。 我是如何坚持更改VIEW LICENSES
和CLOSE
textbuttons 的文本颜色的。 请参阅此图像以进行澄清:
这是我的代码:
...
onTap: () {
showAboutDialog(
context: context,
applicationName: 'bla',
applicationLegalese: 'November 2023',
);
},
到目前为止,我尝试在showAboutDialog
寻找颜色字段,但我找不到任何东西。 我假设我可以更改MaterialApp
ThemeData
的颜色。 不幸的是,我无法找到特定主题来覆盖这些文本按钮的默认样式。
我在我的MaterialApp
ThemeData
尝试了以下ThemeData
来将VIEW LICENSES
和CLOSE
的颜色更改为绿色,但这并没有改变任何东西:
textButtonTheme: TextButtonThemeData(style: ButtonStyle(foregroundColor: MaterialStateProperty.all<Color>(Colors.green))
关于这个的任何想法?
我对这里的答案并不满意,因为所有答案都只显示了 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(),
),
),
你可以使用这个:
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,
更多信息。
我运行这个代码。 经过一些研究,我发现了这种改变颜色的方法。
为此,您需要设置应用程序主主题颜色更改,如下所示
@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',
);
如果您只想更改对话框的颜色而不是整个应用程序的颜色,则必须创建一个新的上下文。 用Theme
和Builder
包围显示对话框的按钮
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.