Flutter 教程(十)主题

如下代码所示,当我们创建一个新 Flutter 项目时,会在 theme 属性中赋值 ThemeData 对象。其中 theme 属性就是主题的入口,该 ThemeData 对象就是设置的对应主题。

scala 复制代码
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

ThemeData

ThemeData 组件包含项目能设置的所有主题样式,其中最常用的属性有primarySwatch、primaryColor、accentColor等27种。ThemeData 常用的属性如下图所示:

注意:ThemeData 包含一些组件的属性,比如 TextField 组件的文本提示颜色 hintColor。如果改变了 ThemeData 中 hintColor 的值则会对全局的 TextField 组件造成影响。但是我们可以在 TextField 组件的 hintStyle 中自定义 hintColor 的颜色,此时在局部小组件中单独设置的主题样式会覆盖全局主题设置的样式

根据上图的属性,我们可以对 ThemeData 的属性进行一些自定义的设置,代码示例如下:

less 复制代码
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        scaffoldBackgroundColor: Colors.deepOrangeAccent,
        accentColor: Colors.yellow,
        primarySwatch: Colors.red,
        hintColor: Colors.blue,
        brightness: Brightness.light,
        buttonTheme: ButtonThemeData(
          textTheme: ButtonTextTheme.accent,
          buttonColor: Colors.greenAccent,
        ),
      ),
      home: MyHomePage(title: '主题设置'),
    );
  }
}

效果如下图所示:

局部主题

上面在 MaterialApp 中设置的主题是全局生效的。如果你只想要在单个组件应用对应的主题,可以使用 Theme 组件。代码示例如下:

less 复制代码
// 方式一
new Theme(
   data: new ThemeData(
     hintColor: Colors.yellow,
   ),
   child: TextField(
     keyboardType: TextInputType.emailAddress,
     decoration: InputDecoration(
       hintText: '提示文本',
       prefixIcon: Icon(Icons.all_inclusive),
     ),
   ),
),

// 方式二
new Theme(
   data:Theme.of(context).copyWith(hintColor: Colors.yellow),
   child: TextField(
     keyboardType: TextInputType.emailAddress,
     decoration: InputDecoration(
       hintText: '提示文本',
       prefixIcon: Icon(Icons.all_inclusive),
     ),
   ),
),

主题换肤

less 复制代码
class ThemePage extends StatefulWidget {
  ThemePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _ThemePageState createState() => _ThemePageState();
}

class _ThemePageState extends State<ThemePage> {

  List<Color> _colorList=[    Colors.yellow,    Colors.greenAccent,    Colors.indigo,    Colors.black38,    Colors.red,    Colors.deepPurpleAccent,    Colors.brown,    Colors.indigo,    Colors.pinkAccent  ];

  Color _color=Colors.yellow;

  @override
  Widget build(BuildContext context) {
    return new Theme(
      data: Theme.of(context).copyWith(primaryColor: _color),
      child: Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: GridView.builder(
          padding: EdgeInsets.only(left: 0,top: 100,right: 0,bottom: 0),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            mainAxisSpacing: 20,
            crossAxisSpacing: 20,
            childAspectRatio: 1,
          ),
          itemBuilder: (BuildContext context,int index){
            return GestureDetector(
              child: Container(
                color: _colorList[index],
              ),
              onTap: (){
                _color=_colorList[index];
                setState(() {

                });
              },
            );
          },
          itemCount: 9,
        ),
      ),
    );
  }
}

参考

相关推荐
好的佩奇1 天前
Dart 之文件读写
android·flutter·dart
Nerd(BUG版)3 天前
Flutter学习day19--MethodChannel打通Dart与原生
学习·flutter
传奇开心果编程3 天前
【六大移动UI框架比较学与练】第5课:交互与导航——事件处理、手势与页面路由实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
恋猫de小郭3 天前
Flutter Analyzer ,这玩意还有这么大的优化空间
android·前端·flutter
hai_android3 天前
从导入视频到上屏出声:Android + FFmpeg 播放器全流程拆解
android·flutter·ffmpeg·kotlin·android studio·音视频·android jetpack
Bowen_J3 天前
Flutter和Dart 网络协议实现
网络协议·flutter·dart
SoaringHeart3 天前
Flutter组件封装:聊天气包图片组件NChatBubble
前端·flutter
传奇开心果编程4 天前
【六大移动UI框架比较学与练】第3课:文本、图片、布局核心组件实战对照
android·flutter·react native·ios·swiftui·移动开发·android jetpack
黑科技iOS上架4 天前
机审友好的iOS混淆方案:无固定二进制特征才能过审4.3
flutter·ios·objective-c·cocoa·审核