在 AppBar 操作列表中全局编辑 TextButton 小部件的颜色?
Globally edit color of TextButton widgets in an AppBar actions list?
在我的根文件中我有:
theme: ThemeData(
appBarTheme: AppBarTheme(
textTheme: TextTheme(
button: TextStyle(color: Colors.white),
),
),
),
在我的自定义 AppBar 文件中:
return AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.transparent,
actions: <Widget>[
TextButton(
child: Text('Sign in'),
onPressed: () {},
),
],
);
但文本仍然是默认的淡蓝色。
您正在 AppBar 中使用 TextButton
,但未在代码中定义其主题。您需要定义 TextButtonTheme 以更改 TextButton 文本的颜色。像这样。
ThemeData(
textbuttonTheme: TextbuttonThemeData(
style: Textbutton.styleFrom(primary: Colors.teal)),
),
return MaterialApp(
theme: ThemeData(
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(primary: Colors.black)
)
),
home: MyWidget(),
);
将 FlatButton 迁移到 TextButton
return MaterialApp(
theme: ThemeData(
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
primary: Colors.black87,
minimumSize: Size(88, 36),
padding: EdgeInsets.symmetric(horizontal: 16.0),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(2.0)),
),
)
)
),
home: MyWidget(),
);
在我的根文件中我有:
theme: ThemeData(
appBarTheme: AppBarTheme(
textTheme: TextTheme(
button: TextStyle(color: Colors.white),
),
),
),
在我的自定义 AppBar 文件中:
return AppBar(
automaticallyImplyLeading: false,
backgroundColor: Colors.transparent,
actions: <Widget>[
TextButton(
child: Text('Sign in'),
onPressed: () {},
),
],
);
但文本仍然是默认的淡蓝色。
您正在 AppBar 中使用 TextButton
,但未在代码中定义其主题。您需要定义 TextButtonTheme 以更改 TextButton 文本的颜色。像这样。
ThemeData(
textbuttonTheme: TextbuttonThemeData(
style: Textbutton.styleFrom(primary: Colors.teal)),
),
return MaterialApp(
theme: ThemeData(
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(primary: Colors.black)
)
),
home: MyWidget(),
);
将 FlatButton 迁移到 TextButton
return MaterialApp(
theme: ThemeData(
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
primary: Colors.black87,
minimumSize: Size(88, 36),
padding: EdgeInsets.symmetric(horizontal: 16.0),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(2.0)),
),
)
)
),
home: MyWidget(),
);