FlutterThemes
Theme主题是预设的软件包,其中包含无涯教程网站或移动应用程序屏幕上的图形外观。
Flutter中的Material Widget也可以使用无涯教程的主题为AppBar,Button,Button,Checkboxes等设置字体样式和背景颜色。
Flutter在创建应用程序时使用默认主题。如果要与整个应用程序共享自定义主题,则需要在MateialApp()小部件下使用ThemeData。
- 通过创建唯一ThemeData
- 通过扩展父主题
创建主题
当无涯教程不想继承任何应用程序颜色或字体样式时,使用第一种方法。在这种情况下,无涯教程将创建一个ThemeData()实例,并将其传递给Theme小部件,如下面的代码片段所示:
Theme( data: ThemeData( accentColor: Colors.blue, ), child: FloatingActionButton( onPressed: () {}, child: Icon(Icons.person), ), );
扩展主题
如果您不想覆盖任何内容,请使用扩展父主题的第二种方法。可以使用copyWith()方法来处理。请参见以下代码段:
Theme( data: Theme.of(context).copyWith(accentColor: Colors.blue), child: FloatingActionButton( onPressed: null, child: Icon(Icons.person), ), );
使用主题
在下面的代码片段中,FloatingActionButton使用此技术返回accentColor。
Container( color: Theme.of(context).accentColor, child: Text( 'Text with a background color', style: Theme.of(context).textTheme.headline, ), );
让无涯教程了解如何在下面的示例中使用Flutter应用程序中的主题。
import 'package:flutter/material.dart'; void main() {runApp(MyApp());} class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( //定义默认亮度和颜色。 brightness: Brightness.dark, primaryColor: Colors.lightBlue, accentColor: Colors.green, //定义默认字体系列。 fontFamily: 'Monotype Coursiva', //定义默认字体系列。 //标题、标题、正文等的文本样式。 textTheme: TextTheme( headline: TextStyle(fontSize: 32.0, fontStyle: FontStyle.italic, fontFamily: 'Hind') ), ), home: MyThemePage(), ); } } class MyThemePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Flutter Theme Example'), ), body: Center( child: Container( color: Theme.of(context).accentColor, child: Text( 'Themes contains the graphical appearances that makes the user interface more attractive.', style: Theme.of(context).textTheme.headline, ), ), ), floatingActionButton: Theme( data: Theme.of(context).copyWith( colorScheme: Theme.of(context).colorScheme.copyWith(secondary: Colors.blue), ), child: FloatingActionButton( onPressed: null, child: Icon(Icons.person), ), ), ); } }
当无涯教程在设备或模拟器中运行应用程序时,无涯教程将看到类似于下面的屏幕截图的UI。
祝学习愉快! (发现内容有误?请选中要编辑的内容 -> 右键 -> 修改 -> 提交!帮助我们改进教程质量)
精选教程推荐
👇 以下精选教程可能对您有帮助,拓展您的技术视野
暂无学习笔记,成为第一个分享的人吧!
您的笔记将帮助成千上万的学习者