App 开发过程中,肯定希望给用户带来一致的体验,这其中最基础的就是色调、字体保持一致。在 Flutter 中,可以设置全局的主题色调和字体,从而在其他页面引用主色调和字体,实现页面展示层面的一致。

    Flutter 的主题色和字体可以在MaterialApp 中设置,即在 main.dart 的入口返回的 MaterialApp 组件统一设置全局的主色调和字体。如下面的代码所示:

    1. class MyApp extends StatelessWidget {
    2. @override
    3. Widget build(BuildContext context) {
    4. return MaterialApp(
    5. title: 'App 框架',
    6. theme: ThemeData(
    7. primaryColor: Colors.blue,
    8. accentColor: Colors.blue[600],
    9. textTheme: TextTheme(
    10. headline1: TextStyle(
    11. fontSize: 36.0, fontWeight: FontWeight.bold, color: Colors.white),
    12. headline2: TextStyle(
    13. fontSize: 32.0, fontWeight: FontWeight.w400, color: Colors.white),
    14. headline3: TextStyle(
    15. fontSize: 28.0, fontWeight: FontWeight.w400, color: Colors.white),
    16. headline4: TextStyle(
    17. fontSize: 24.0, fontWeight: FontWeight.w400, color: Colors.white),
    18. headline6: TextStyle(
    19. fontSize: 14.0, fontWeight: FontWeight.w200, color: Colors.white),
    20. bodyText1: TextStyle(
    21. fontSize: 20.0,
    22. fontWeight: FontWeight.w200,
    23. ),
    24. ),
    25. fontFamily: 'Georgia',
    26. ),
    27. home: AppHomePage(),
    28. );
    29. }
    30. }

    通过 MateriaApp 的 theme 属性,构建 ThemeData 来配置全局主题。其中ThemeData常用的属性如下所示:

    • brightness:为 Brightness 枚举,包括 dark 和 light 两种模式,其中 dark 对应的是深色模式(即夜间模式),light 对应浅色模式。
    • primaryColor:主色调,设置后导航栏就会变成主色调颜色。注意的是导航栏的字体颜色会根据主色调和 brightness 自动计算显示的颜色是偏浅色还是深色。
    • accentColor:辅助色,根据需要设置。
    • textTheme:文字主体。早期版本的 flutter 设置的比较少,新版本可能是为了支持Web端,字体的属性设置基本和 html 的保持一致了,包括 headline1到 headline6,bodyText1,感觉就是对应 html 中的 h1-h6和 body 的字体。各级字体均可以通过构建 TextStyle 来设置对应的字体参数。
    • fontFamily:字体族。

    在应用中可以通过 Theme.of(context)获取当前主体,再获取对应的属性来继承主题的色调或字体。如下面的代码的 Text 的样式就继承了主体的bodyText1的字体特性。

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. body: Center(
    5. child: Text(
    6. '岛',
    7. style: Theme.of(context).textTheme.bodyText1,
    8. ),
    9. ),
    10. );
    11. }

    而在BottomNavigationBar中的 selectedItemColor(选择颜色)则继承了主色调。

    1. @override
    2. Widget build(BuildContext context) {
    3. return Scaffold(
    4. appBar: AppBar(
    5. title: Text('岛上码农', style: Theme.of(context).textTheme.headline4),
    6. ),
    7. body: IndexedStack(
    8. index: _index,
    9. children: _homeWidgets,
    10. ),
    11. bottomNavigationBar: BottomNavigationBar(
    12. type: BottomNavigationBarType.fixed,
    13. currentIndex: _index,
    14. onTap: _onBottomNagigationBarTapped,
    15. selectedItemColor: Theme.of(context).primaryColor,
    16. items: [
    17. _getBottomNavItem(
    18. '动态', 'images/dynamic.png', 'images/dynamic-hover.png'),
    19. _getBottomNavItem(
    20. ' 消息', 'images/message.png', 'images/message-hover.png'),
    21. _getBottomNavItem(
    22. '分类浏览', 'images/category.png', 'images/category-hover.png'),
    23. _getBottomNavItem(
    24. '个人中心', 'images/mine.png', 'images/mine-hover.png'),
    25. ],
    26. ),
    27. );
    28. }

    通过这种方式可以在 main.dart 中的 MaterialApp 中统一配置色调和字体达到全局一致的目的。如果想要调整色调和字体,只需要在一处修改即可。最终结果如下图所示(有图更改了主题色为绿色)。
    zhusediao.jpg

    有强迫症的同学可能会发现状态栏的颜色是黑色的,这个该如何修改呢?很简单,对 AppBar的属性设置一下 brightness 属性即可:

    1. return Scaffold(
    2. appBar: AppBar(
    3. title: Text('岛上码农', style: Theme.of(context).textTheme.headline4),
    4. brightness: Brightness.dark,
    5. ),
    6. //...

    以上即为 Flutter App 的主题色与字体的设置,实际另一种操作方式也可以使用全局常量的方式,约定好整个工程使用的主色调,辅助色,字体也能达到目的。下一篇介绍如何构建列表,欢迎点赞鼓励!