zoukankan      html  css  js  c++  java
  • 02、Flutter主题

    Flutter主题

    入门程序

    在学习任意一门语言的开始我们都有一个入门程序,Flutter也不例外:

    import 'package:flutter/material.dart';
    void main() => runApp(MyApp());
    class MyApp extends StatelessWidget{
        @override
        Widget build(BuildContext context){
            return MaterialApp(
                title: 'Welcome to Flutter',
                home: Scaffold(
                    appBar: AppBar(
                        title: Text("Welcome to Flutter"),
                    ), 
                    body: Center(
                        child: Text("Hello World");
                    ),
                )
            );
        }
    }
    

    在Flutter中的/lib目录下都有一个main.dart文件,该文件里包含一个main()函数。它是Dart程序的入口,也就是在程序运行的时候第一个执行的函数。

    主题样式

    在Flutter中每个.dart文件都会导入flutter/material.dart包,它是实现Material Design设计风格的基础包。

    import 'package:flutter/ material.dart ’;
    

    Flutter主题

    为了在整个应用中使用同一套颜色和字体样式,可以使用“主题”这种方式。定义主题有两种方式:全局主题或使用There来定义用用程序局部的颜色和字体样式。

    定义主题之后,就可以在编写的Widget中使用它,Flutter的Material Widgets将使用主题为AppBars、Buttons、CheckBox等设置背景颜色和字体样式。

    factory ThemeData({
      Brightness brightness, // 应用整体主题的亮度。用于按钮之类的小部件,以确定在不使用主色或强调色时选择什么颜色。
      MaterialColor primarySwatch,// 定义一个单一的颜色以及十个色度的色块。
      Color primaryColor, // 应用程序主要部分的背景颜色(toolbars、tab bars 等)
      Brightness primaryColorBrightness, // primaryColor的亮度。用于确定文本的颜色和放置在主颜色之上的图标(例如工具栏文本)。
      Color primaryColorLight, // primaryColor的浅色版
      Color primaryColorDark, // primaryColor的深色版
      Color accentColor, // 小部件的前景色(旋钮、文本、覆盖边缘效果等)。
      Brightness accentColorBrightness, // accentColor的亮度。
      Color canvasColor, //  MaterialType.canvas 的默认颜色
      Color scaffoldBackgroundColor, // Scaffold的默认颜色。典型Material应用程序或应用程序内页面的背景颜色。
      Color bottomAppBarColor, // BottomAppBar的默认颜色
      Color cardColor, // Card的颜色
      Color dividerColor, // Divider和PopupMenuDivider的颜色,也用于ListTile之间、DataTable的行之间等。
      Color highlightColor, // 选中在泼墨动画期间使用的突出显示颜色,或用于指示菜单中的项。
      Color splashColor,  // 墨水飞溅的颜色。InkWell
      InteractiveInkFeatureFactory splashFactory, // 定义由InkWell和InkResponse反应产生的墨溅的外观。
      Color selectedRowColor, // 用于突出显示选定行的颜色。
      Color unselectedWidgetColor, // 用于处于非活动(但已启用)状态的小部件的颜色。例如,未选中的复选框。通常与accentColor形成对比。也看到disabledColor。
      Color disabledColor, // 禁用状态下部件的颜色,无论其当前状态如何。例如,一个禁用的复选框(可以选中或未选中)。
      Color buttonColor, // RaisedButton按钮中使用的Material 的默认填充颜色。
      ButtonThemeData buttonTheme, // 定义按钮部件的默认配置,如RaisedButton和FlatButton。
      Color secondaryHeaderColor, // 选定行时PaginatedDataTable标题的颜色。
      Color textSelectionColor, // 文本框中文本选择的颜色,如TextField
      Color cursorColor, // 文本框中光标的颜色,如TextField
      Color textSelectionHandleColor,  // 用于调整当前选定的文本部分的句柄的颜色。
      Color backgroundColor, // 与主色形成对比的颜色,例如用作进度条的剩余部分。
      Color dialogBackgroundColor, // Dialog 元素的背景颜色
      Color indicatorColor, // 选项卡中选定的选项卡指示器的颜色。
      Color hintColor, // 用于提示文本或占位符文本的颜色,例如在TextField中。
      Color errorColor, // 用于输入验证错误的颜色,例如在TextField中
      Color toggleableActiveColor, // 用于突出显示Switch、Radio和Checkbox等可切换小部件的活动状态的颜色。
      String fontFamily, // 文本字体
      TextTheme textTheme, // 文本的颜色与卡片和画布的颜色形成对比。
      TextTheme primaryTextTheme, // 与primaryColor形成对比的文本主题
      TextTheme accentTextTheme, // 与accentColor形成对比的文本主题。
      InputDecorationTheme inputDecorationTheme, // 基于这个主题的 InputDecorator、TextField和TextFormField的默认InputDecoration值。
      IconThemeData iconTheme, // 与卡片和画布颜色形成对比的图标主题
      IconThemeData primaryIconTheme, // 与primaryColor形成对比的图标主题
      IconThemeData accentIconTheme, // 与accentColor形成对比的图标主题。
      SliderThemeData sliderTheme,  // 用于呈现Slider的颜色和形状
      TabBarTheme tabBarTheme, // 用于自定义选项卡栏指示器的大小、形状和颜色的主题。
      CardTheme cardTheme, // Card的颜色和样式
      ChipThemeData chipTheme, // Chip的颜色和样式
      TargetPlatform platform, 
      MaterialTapTargetSize materialTapTargetSize, // 配置某些Material部件的命中测试大小
      PageTransitionsTheme pageTransitionsTheme, 
      AppBarTheme appBarTheme, // 用于自定义Appbar的颜色、高度、亮度、iconTheme和textTheme的主题。
      BottomAppBarTheme bottomAppBarTheme, // 自定义BottomAppBar的形状、高度和颜色的主题。
      ColorScheme colorScheme, // 拥有13种颜色,可用于配置大多数组件的颜色。
      DialogTheme dialogTheme, // 自定义Dialog的主题形状
      Typography typography, // 用于配置TextTheme、primaryTextTheme和accentTextTheme的颜色和几何TextTheme值。
      CupertinoThemeData cupertinoOverrideTheme 
    })
    

    如果没有提供主题,Flutter将创建一个默认主题,主题数据的示例代码如下:

    new MaterialApp(
        title:title,
        theme: new ThemeData(
            brightness: Brightness.dark,
            primaryColor: Colors.lightBlue[800],
            accentColor: Colors.cyan[600],
        ),
    );
    

    局部主题

    如果我们想在应用程序的某一部分使用特殊的颜色,那么就需要覆盖全局的主题。有两种方法可以解决该问题。

    1. 创建特有的主题数据或扩展父主题。
    new Theme(
        // 创建一个特有的主题数据
        data: new ThemeData(
            accentColor: Colors.yellow,
        ),
        child: new FloatingActionButton(
            onPressed:(){}
            child: new Icon(Icons.add),
        ),
    );
    
    1. 扩展父主题的方式无需覆盖所有的主题属性,可以通过copyWith方法实现。
    new Theme(
        // 覆盖accentColor为Colors.yellow
        data: Theme.of(context).copyWith(accentColor: Colors.yellow),
        onPressed:null,
        child: new Icon(Icons.add),
      ),  
    );
    

    使用主题

    函数Theme.of(context)可以通过上下文来获取主题,方法是查找最近的主题,如果找不到就会找整个应用的主题。

    下面看简单的实例,应用的主题颜色定义为绿色,界面中间再加一个带有背景色的文本。

    import 'package:flutter/material.dart';
    import 'package:flutter/foundation.dart';
    void main() {
      runApp(new MyApp());
    }
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final appName = "自定义主题";
        return MaterialApp(
          title: appName,
          theme: ThemeData(
            // 应用程序整体主题的亮度
            brightness: Brightness.light,
            // App主要部分的背景色
            primaryColor: Colors.lightGreen[600],
            // 前景色(文本、按钮等)
            accentColor: Colors.orange[600],
          ),
          home: MyHomePage(
            title: appName,
          ),
        );
      }
    }
    class MyHomePage extends StatelessWidget {
      final String title;
      MyHomePage({Key key, @required this.title}) : super(key: key);
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(title),
          ),
          body: Center(
            child: Container(
              // 获取主题的accentColor
              color: Theme.of(context).accentColor,
              child: Text(
                '带有背景颜色的文本组件',
                style: Theme.of(context).textTheme.title,
              ),
            ),
          ),
          floatingActionButton: Theme(
            // 使用copyWith的方式获取accentColor
            data: Theme.of(context).copyWith(accentColor: Colors.grey),
            child: FloatingActionButton(
              onPressed: null,
              child: Icon(Icons.computer),
            ),
          ),
        );
      }
    }
    

    无状态和有状态组件

    无状态组件(StatelessWidget)是不可变的,这意味着它的属性不能改变,所有的值都是最终的。

    有状态组件(StatefulWidget)持有的状态可能在Widget生命周期中发生变化,实现一个StatefulWidgt至少需要两个类:

    一个StatefulWidget类。
    一个State类。StatefulWidgt类本身是不变的,但是State类在Widget生命周期中始终存在。

    Flutter官方给出一个有状态组件的实例,点击右下角+号按钮,应用界面中间的数字会加1。

    import 'package:flutter/material.dart';
    void main() {
      runApp(MyApp());
    }
    // MyApp不需要做状态处理,所以此组件继承StatelessWidget即可
    class MyApp extends StatelessWidget {
      // 这个组件是整个应用的主组件
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            // 自定义主题
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    // 主页需要继承自StatefulWidget
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
      // 标题
      final String title;
      // 必须重写createState()方法
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    // 状态类必须继承State类,注意后面需要指定为MyHomePage
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0; // 计数器
      void _incrementCounter() {
        // 调用State类里的setState方法来更改状态值,使得计数器加1
        setState(() {
          _counter++;
        });
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          // 居中布局
          body: Center(
            // 垂直布局
            child: Column(
              // 主轴居中对齐
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter', // 绑定计数器的值
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter, // 按下+号按钮调用自增函数
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }
    
  • 相关阅读:
    Discuz X 2.5 点点(伪静态)
    jq 、xml 省市级联动
    php memcache 初级使用(2)
    关于windows虚拟内存管理的页目录自映射
    SharePoint 2010 网络上的开发经验和资源
    SharePoint 2010 Reporting Services 报表服务器正在内置 NT AUTHORITY\SYSTEM 账户下运行 解决方法
    SharePoint 2010 Reporting Services 报表服务器无法解密用于访问报表服务器数据库中的敏感数据或加密数据的对称密钥 解决方法
    Active Directory Rights Management Services (AD RMS)无法检索证书层次结构。 解决方法
    SharePoint 2010 Reporting Services 报表服务器实例没有正确配置 解决方法
    SharePoint 2010 页面引用 Reporting Services 展现 List 报表
  • 原文地址:https://www.cnblogs.com/pengjingya/p/14928317.html
Copyright © 2011-2022 走看看