zoukankan      html  css  js  c++  java
  • flutter 局部主题

    如果我们想在应用程序的一部分中覆盖应用程序的全局的主题,我们可以将要覆盖得部分封装在一个ThemeWidget中。

    有两种方法可以解决这个问题:创建特有的ThemeData或扩展父主题。

    1.创建特有的ThemeData

    如果我们不想继承任何应用程序的颜色或字体样式,我们可以通过new ThemeData()创建一个实例并将其传递给Theme Widget。

    new Theme(
      // Create a unique theme with "new ThemeData"
      data: new ThemeData(
        accentColor: Colors.yellow,
      ),
      child: new FloatingActionButton(
        onPressed: () {},
        child: new Icon(Icons.add),
      ),
    );

    2.扩展父主题

    扩展父主题时无需覆盖所有的主题属性,我们可以通过使用copyWith方法来实现。

    new Theme(
      // Find and Extend the parent theme using "copyWith". Please see the next 
      // section for more info on `Theme.of`.
      data: Theme.of(context).copyWith(accentColor: Colors.yellow),
      child: new FloatingActionButton(
        onPressed: null,
        child: new Icon(Icons.add),
      ),
    );

    具体代入例子:

    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(new MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final appName = 'Custom Themes';
    
        return new MaterialApp(
          title: appName,
          theme: new ThemeData(
            brightness: Brightness.dark,
            primaryColor: Colors.lightBlue[800],
            accentColor: Colors.cyan[600],
          ),
          home: new 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 new Scaffold(
          appBar: new AppBar(
            title: new Text(title),
          ),
          body: new Center(
            child: new Container(
              color: Theme.of(context).accentColor,
              child: new Text(
                'Text with a background color',
                style: Theme.of(context).textTheme.title,
              ),
            ),
          ),
          floatingActionButton: new Theme(
            data: Theme.of(context).copyWith(accentColor: Colors.yellow),
            child: new FloatingActionButton(
              onPressed: null,
              child: new Icon(Icons.add),
            ),
          ),
        );
      }
    }
  • 相关阅读:
    前端如何进阶架构师
    NPOI使用记录
    ArcGis 中空间数据的插入与更新
    (转载).net 缓存处理
    ASP.NET(c#)实现重定向的三种方法的总结
    数据库关联表之间的更新语句
    C#net多线程多文件压缩下载
    关于写文件流的情况
    C# Class获取项目的绝对路径
    C# .net中DatailsView的JS简易版
  • 原文地址:https://www.cnblogs.com/gaozhang12345/p/11991514.html
Copyright © 2011-2022 走看看