短视频系统源码,Flutter 设置 App 的主色调与字体
短视频系统源码,Flutter 设置 App 的主色调与字体实现的相关代码
Flutter 的主题色和字体可以在MaterialApp 中设置,即在 main.dart 的入口返回的 MaterialApp 组件统一设置全局的主色调和字体。如下面的代码所示:
1 | class MyApp extends StatelessWidget {<br> @override <br> Widget build(BuildContext context) {<br> return MaterialApp(<br> title: 'App 框架' ,<br> theme: ThemeData(<br> primaryColor: Colors.blue,<br> accentColor: Colors.blue[ 600 ],<br> textTheme: TextTheme(<br> headline1: TextStyle(<br> fontSize: 36.0 , fontWeight: FontWeight.bold, color: Colors.white),<br> headline2: TextStyle(<br> fontSize: 32.0 , fontWeight: FontWeight.w400, color: Colors.white),<br> headline3: TextStyle(<br> fontSize: 28.0 , fontWeight: FontWeight.w400, color: Colors.white),<br> headline4: TextStyle(<br> fontSize: 24.0 , fontWeight: FontWeight.w400, color: Colors.white),<br> headline6: TextStyle(<br> fontSize: 14.0 , fontWeight: FontWeight.w200, color: Colors.white),<br> bodyText1: TextStyle(<br> fontSize: 20.0 ,<br> fontWeight: FontWeight.w200,<br> ),<br> ),<br> fontFamily: 'Georgia' ,<br> ),<br> home: AppHomePage(),<br> );<br> }<br>} |
通过 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 <br> Widget build(BuildContext context) {<br> return Scaffold(<br> body: Center(<br> child: Text(<br> '岛' ,<br> style: Theme.of(context).textTheme.bodyText1,<br> ),<br> ),<br> );<br> } |
而在BottomNavigationBar中的 selectedItemColor(选择颜色)则继承了主色调。
1 | @override <br> Widget build(BuildContext context) {<br> return Scaffold(<br> appBar: AppBar(<br> title: Text( '岛上码农' , style: Theme.of(context).textTheme.headline4),<br> ),<br> body: IndexedStack(<br> index: _index,<br> children: _homeWidgets,<br> ),<br> bottomNavigationBar: BottomNavigationBar(<br> type: BottomNavigationBarType.fixed,<br> currentIndex: _index,<br> onTap: _onBottomNagigationBarTapped,<br> selectedItemColor: Theme.of(context).primaryColor,<br> items: [<br> _getBottomNavItem(<br> '动态' , 'images/dynamic.png' , 'images/dynamic-hover.png' ),<br> _getBottomNavItem(<br> ' 消息' , 'images/message.png' , 'images/message-hover.png' ),<br> _getBottomNavItem(<br> '分类浏览' , 'images/category.png' , 'images/category-hover.png' ),<br> _getBottomNavItem(<br> '个人中心' , 'images/mine.png' , 'images/mine-hover.png' ),<br> ],<br> ),<br> );<br> } |
以上就是 短视频系统源码,Flutter 设置 App 的主色调与字体实现的相关代码,更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现