[Flutter]使用主題

使用主題可以在App里面共享顏色和字體樣式。在Flutter里面有兩種方式來使用主題,一種是全局范圍的、一種是使用Theme Widget, Theme Widget可以在App的某個(gè)部分使用主題。全局的主題其實(shí)也就是MaterialAppTheme 做為根widget了。

主題定義好后,就可以在Widgets里面使用了。

創(chuàng)建一個(gè)全局主題

MaterialApp 接收一個(gè)theme的參數(shù),類型為ThemeData,為App提供統(tǒng)一的顏色和字體。支持的參數(shù)可以在這里查看

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

創(chuàng)建一個(gè)局部主題

如果想為某個(gè)頁面使用不同于App的風(fēng)格,可以使用Theme來覆蓋App的主題.

new Theme(
    data: new ThemeData(
        accentColor: Colors.yellow,
    ),
    child: new Text('Hello World'),
);

擴(kuò)展App的主題

如果你不想覆蓋所有的樣式,可以繼承App的主題,只覆蓋部分樣式,使用copyWith方法。

new Theme(
    data: Theme.of(context).copyWith(accentColor: Colors.yellow),
    child: new Text('use copyWith method'),
);

使用主題

創(chuàng)建好主題后,要如何使用呢,在Widget的構(gòu)造方法里面通過Theme.of(context)方法來調(diào)用。

Theme.of(context) 會(huì)查找Widget 樹,并返回最近的一個(gè)Theme對象。如果父層級上有Theme對象,則返回這個(gè)Theme,如果沒有,就返回App的Theme

new Container(
    color: Theme.of(context).accentColor,
    chile: new Text(
        'Text with a background color',
        style: Theme.of(context).textTheme.title,
    ),
);

完整例子代碼

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),
        ),
      ),
    );
  }
}

關(guān)于學(xué)習(xí)

flutter的學(xué)習(xí)文章都整理在這個(gè)github倉庫

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評論 25 708
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,380評論 0 17
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,692評論 19 139
  • 雞腿總能勾起我們的饞蟲,它方便好做、吃法多樣、肉質(zhì)細(xì)嫩……無論是外皮脆焦、內(nèi)里香甜的照燒雞腿,還是浸透麻油味道,鮮...
    一坪海岸線閱讀 505評論 0 2
  • Cookie Cookie,指某些網(wǎng)站為了辨別用戶身份、進(jìn)行session跟蹤而儲(chǔ)存在用戶本地終端上的數(shù)據(jù)(通常經(jīng)...
    幻想無極閱讀 534評論 0 2

友情鏈接更多精彩內(nèi)容