),
home: AppHomePage(),
);
}
}
通过 MateriaApp 的 theme 属性,构建 ThemeData 来配置全局主题。其中ThemeData常用的属性如下所示:
在应用中可以通过 Theme.of(context)获取当前主体,再获取对应的属性来继承主题的色调或字体。如下面的代码的 Text 的样式就继承了主体的bodyText1的字体特性。
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(
‘岛’,
style: Theme.of(context).textTheme.bodyText1,
),
),
);
}
而在BottomNavigationBar中的 selectedItemColor(选择颜色)则继承了主色调。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘岛上码农’, style: Theme.of(context).textTheme.headline4),
),
body: IndexedStack(
index: _index,
children: _homeWidgets,
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: _index,
onTap: _onBottomNagigationBarTapped,
selectedItemColor: Theme.of(context).primaryColor,
items: [
_getBottomNavItem(
‘动态’, ‘images/dynamic.png’, ‘images/dynamic-hover.png’),
_getBottomNavItem(
’ 消息’, ‘images/message.png’, ‘images/message-hover.png’),
_getBottomNavItem(
‘分类浏览’, ‘images/category.png’, ‘images/category-hover.png’),
_getBottomNavItem(
‘个人中心’, ‘images/mine.png’, ‘images/mine-hover.png’),
],
),
);
}
通过这种方式可以在 main.dart 中的 MaterialApp 中统一配置色调和字体达到全局一致的目的。如果想要调整色调和字体,只需要在一处修改即可。最终结果如下图所示(有图更改了主题色为绿色)。
有强迫症的同学可能会发现状态栏的颜色是黑色的,这个该如何修改呢?很简单,对 AppBar的属性设置一下 brightness 属性即可:
return Scaffold(
appBar: AppBar(
title: Text(‘岛上码农’, style: Theme.of(context).textTheme.headline4),
brightness: Brigh
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
tness.dark,
),
//…
以上即为 Flutter App 的主题色与字体的设置,实际另一种操作方式也可以使用全局常量的方式,约定好整个工程使用的主色调,辅助色,字体也能达到目的。下一篇介绍如何构建列表,欢迎点赞鼓励!
题色与字体的设置,实际另一种操作方式也可以使用全局常量的方式,约定好整个工程使用的主色调,辅助色,字体也能达到目的。下一篇介绍如何构建列表,欢迎点赞鼓励!