Flutter 如何使用主题更改AppBar的文本颜色、FAB的图标颜色?
我可以将Flutter 如何使用主题更改AppBar的文本颜色、FAB的图标颜色?,flutter,Flutter,我可以将AppBar的背景色设置为Colors.amber。这会自动将文本颜色设置为黑色。我知道可能出现的可访问性问题,但无论如何,我希望文本颜色为白色 我仍然可以从AppBar设置文本颜色,但我想普遍设置 以下是我在应用程序中使用的主题 title: 'Flutter Demo', theme: new ThemeData( primarySwatch: Colors.amber, textTheme: Theme.of(context).textTheme.apply( bo
AppBar
的背景色设置为Colors.amber
。这会自动将文本颜色设置为黑色。我知道可能出现的可访问性问题,但无论如何,我希望文本颜色为白色
我仍然可以从AppBar
设置文本颜色,但我想普遍设置
以下是我在应用程序中使用的主题
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.amber,
textTheme: Theme.of(context).textTheme.apply(
bodyColor: Colors.white,
displayColor: Colors.white,
),
),
下面是设置AppBar标题颜色的方法
return new MaterialApp(
theme: Theme.of(context).copyWith(
accentIconTheme: Theme.of(context).accentIconTheme.copyWith(
color: Colors.white
),
accentColor: Colors.amber,
primaryColor: Colors.amber,
primaryIconTheme: Theme.of(context).primaryIconTheme.copyWith(
color: Colors.white
),
primaryTextTheme: Theme
.of(context)
.primaryTextTheme
.apply(bodyColor: Colors.white)),
home: Scaffold(
appBar: AppBar(
title: Text("Theme Demo"),
leading: IconButton(
onPressed: (){},
icon: Icon(Icons.menu),
),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
),
),
);
我认为最直接的方法是调整主题的标题颜色:
theme: new ThemeData(
primarySwatch: Colors.grey,
primaryTextTheme: TextTheme(
headline6: TextStyle(
color: Colors.white
)
)
)
我使用了稍微不同的技术,我没有使用主题,我只是定制了它的外观,所以当我创建它时,它看起来像这样:
appBar: new AppBar(
iconTheme: IconThemeData(
color: Colors.white
),
title: const Text('Saved Suggestions', style: TextStyle(
color: Colors.white
)),
backgroundColor: Colors.pink,
),
我将写下
主题数据
属性的更改会产生什么影响
这里写的内容是一种尽可能不影响其他小部件的方式
如果要更改appbar标题的颜色
primaryTextTheme: TextTheme(
title: TextStyle(
color: Colors.white
)
),
如果要更改appbar的图标颜色
primaryIconTheme: const IconThemeData.fallback().copyWith(
color: Colors.white,
),
如果你想改变晶圆厂的图标颜色
accentIconTheme: const IconThemeData.fallback().copyWith(
color: Colors.white,
),
此外,当您想要更改晶圆厂的颜色时。这是不可能的,只有通过属性的主题数据。 所以您需要直接指定它。但是,最好使用
Theme.of(context)
在第一次调用main.dart文件时运行的小部件中,可以添加命名参数主题,该主题使您能够添加全局样式 在小部件的构建方法中
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: _buildLightTheme(),
title: 'title of app',
home: LoginPage(app: app),
initialRoute: '/login',
routes: <String, WidgetBuilder>{
"/login": (BuildContext context) => LoginPage(app: app,)
});
}
对于appBarTheme,我有一个单独的方法\u appBarTheme
AppBarTheme _appBarTheme(){
return AppBarTheme(
elevation: 0.0,
color: kUndaGreen,
iconTheme: IconThemeData(
color: Colors.white,
),);
}
简单、高效、代码少的方法。使用具有所需颜色的灯光主题
theme: ThemeData.light().copyWith(
accentColor: Colors.amber,
primaryColor: Colors.amber,
),
首先,我想让你知道,有3种方法来设置颜色在颤振 所以你问你想在应用程序栏中设置颜色或文本。所以,若您在文本方法的样式属性中设置颜色,它就会工作。让我告诉你它是如何工作的。我还将向您展示3种设置颜色的方法。是否使用主题属性并不重要,因为设置文本的颜色是不同的。这就是我在示例中没有使用主题属性的方式 第1条: 第2条:
Scaffold(
appBar: AppBar(
title: Text("App Name",
style: TextStyle(color: Color(0xffffff),));
第三:
您可以使用AppBarTheme进行设置
AppBarTheme _appBarTheme(){
return AppBarTheme(
elevation: 0.0,
color: kUndaGreen,
iconTheme: IconThemeData(
color: Colors.white,
),);
}
主题:新主题数据(
textTheme:Theme.of(context).textTheme.apply(
阿帕巴瑟姆:阿帕巴瑟姆(
textTheme:textTheme(
//中心文本样式
标题6:文本样式(
颜色:颜色。黑色
),
//侧边文字样式
bodyText2:TextStyle(颜色:Colors.black)
)
)
),
),
AppBar的标题使用标题6
和浮动操作栏使用原始样本的颜色。虽然它没有在TextTheme
中记录,但我测试了它。例如:要在AppBar
中使用红色floatingAction按钮
和蓝色标题文本,您的主题
内部MaterialApp
应如下所示:
MaterialApp(
theme: ThemeData(
primarySwatch: Colors.red,
appBarTheme: AppBarTheme(
textTheme: TextTheme(
headline6: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
fontSize: 28.0,
),
),
),
),
)
您可以通过扩展appBar类来创建自己的自定义appBar,然后在我不知道AppBarTheme
的任何地方使用它,这非常有帮助。非常感谢你!这只适用于一次性,但OP特别询问如何使用主题实现这一点。此解决方案不再有效,因为触发的错误如下:“title”已弃用,不应使用。这是2014版材料设计中使用的术语。现代术语是头条新闻。此功能在v1.13.8之后被弃用。。尝试用替换项替换不推荐使用的成员。您如何知道AppBar使用的是headline6
?有文件吗。。?如果有,您能否指出???@Ramesh-X文档标题6“用于应用程序栏和对话框中的主要文本(例如,AppBar.title和AlertDialog.title)”来源:TextTheme.title
称为TextTheme.headline6
nowTextTheme.headline6
将appbar的默认字体大小更改为较小的字体大小问题是如何设置颜色。这不是解决问题的办法
Scaffold(
appBar: AppBar(
title: Text("App Name",
style: TextStyle(color: Color(0xffffff),));
Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: Text("App Name",
style: TextStyle(color: Color.fromRGBO(0, 0, 0, 0, 0),));
MaterialApp(
theme: ThemeData(
primarySwatch: Colors.red,
appBarTheme: AppBarTheme(
textTheme: TextTheme(
headline6: TextStyle(
color: Colors.blue,
fontWeight: FontWeight.bold,
fontSize: 28.0,
),
),
),
),
)