欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Flutter 如何設(shè)置App的主色調(diào)與字體

 更新時(shí)間:2021年05月21日 08:32:51   作者:島上碼農(nóng)  
App 開發(fā)過(guò)程中,肯定希望給用戶帶來(lái)一致的體驗(yàn),這其中最基礎(chǔ)的就是色調(diào)、字體保持一致。在 Flutter 中,可以設(shè)置全局的主題色調(diào)和字體,從而在其他頁(yè)面引用主色調(diào)和字體,實(shí)現(xiàn)頁(yè)面展示層面的一致。

Flutter 的主題色和字體可以在MaterialApp 中設(shè)置,即在 main.dart 的入口返回的 MaterialApp 組件統(tǒng)一設(shè)置全局的主色調(diào)和字體。如下面的代碼所示:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'App 框架',
      theme: ThemeData(
        primaryColor: Colors.blue,
        accentColor: Colors.blue[600],
        textTheme: TextTheme(
          headline1: TextStyle(
              fontSize: 36.0, fontWeight: FontWeight.bold, color: Colors.white),
          headline2: TextStyle(
              fontSize: 32.0, fontWeight: FontWeight.w400, color: Colors.white),
          headline3: TextStyle(
              fontSize: 28.0, fontWeight: FontWeight.w400, color: Colors.white),
          headline4: TextStyle(
              fontSize: 24.0, fontWeight: FontWeight.w400, color: Colors.white),
          headline6: TextStyle(
              fontSize: 14.0, fontWeight: FontWeight.w200, color: Colors.white),
          bodyText1: TextStyle(
            fontSize: 20.0,
            fontWeight: FontWeight.w200,
          ),
        ),
        fontFamily: 'Georgia',
      ),
      home: AppHomePage(),
    );
  }
}

通過(guò) MateriaApp 的 theme 屬性,構(gòu)建 ThemeData 來(lái)配置全局主題。其中ThemeData常用的屬性如下所示:

  • brightness:為 Brightness 枚舉,包括 dark 和 light 兩種模式,其中 dark 對(duì)應(yīng)的是深色模式(即夜間模式),light 對(duì)應(yīng)淺色模式。
  • primaryColor:主色調(diào),設(shè)置后導(dǎo)航欄就會(huì)變成主色調(diào)顏色。注意的是導(dǎo)航欄的字體顏色會(huì)根據(jù)主色調(diào)和 brightness 自動(dòng)計(jì)算顯示的顏色是偏淺色還是深色。
  • accentColor:輔助色,根據(jù)需要設(shè)置。
  • textTheme:文字主體。早期版本的 flutter 設(shè)置的比較少,新版本可能是為了支持Web端,字體的屬性設(shè)置基本和 html 的保持一致了,包括 headline1到 headline6,bodyText1,感覺就是對(duì)應(yīng) html 中的 h1-h6和 body 的字體。各級(jí)字體均可以通過(guò)構(gòu)建 TextStyle 來(lái)設(shè)置對(duì)應(yīng)的字體參數(shù)。
  • fontFamily:字體族。

在應(yīng)用中可以通過(guò) Theme.of(context)獲取當(dāng)前主體,再獲取對(duì)應(yīng)的屬性來(lái)繼承主題的色調(diào)或字體。如下面的代碼的 Text 的樣式就繼承了主體的bodyText1的字體特性。

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(
          '島',
          style: Theme.of(context).textTheme.bodyText1,
        ),
      ),
    );
  }

而在BottomNavigationBar中的 selectedItemColor(選擇顏色)則繼承了主色調(diào)。

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('島上碼農(nóng)', style: Theme.of(context).textTheme.headline4),
      ),
      body: IndexedStack(
        index: _index,
        children: _homeWidgets,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _index,
        onTap: _onBottomNagigationBarTapped,
        selectedItemColor: Theme.of(context).primaryColor,
        items: [
          _getBottomNavItem(
              '動(dòng)態(tài)', 'images/dynamic.png', 'images/dynamic-hover.png'),
          _getBottomNavItem(
              ' 消息', 'images/message.png', 'images/message-hover.png'),
          _getBottomNavItem(
              '分類瀏覽', 'images/category.png', 'images/category-hover.png'),
          _getBottomNavItem(
              '個(gè)人中心', 'images/mine.png', 'images/mine-hover.png'),
        ],
      ),
    );
  }

通過(guò)這種方式可以在 main.dart 中的 MaterialApp 中統(tǒng)一配置色調(diào)和字體達(dá)到全局一致的目的。如果想要調(diào)整色調(diào)和字體,只需要在一處修改即可。最終結(jié)果如下圖所示(有圖更改了主題色為綠色)。

有強(qiáng)迫癥的同學(xué)可能會(huì)發(fā)現(xiàn)狀態(tài)欄的顏色是黑色的,這個(gè)該如何修改呢?很簡(jiǎn)單,對(duì) AppBar的屬性設(shè)置一下 brightness 屬性即可:

return Scaffold(
      appBar: AppBar(
        title: Text('島上碼農(nóng)', style: Theme.of(context).textTheme.headline4),
        brightness: Brightness.dark,
      ),
  //...

以上即為 Flutter App 的主題色與字體的設(shè)置,實(shí)際另一種操作方式也可以使用全局常量的方式,約定好整個(gè)工程使用的主色調(diào),輔助色,字體也能達(dá)到目的。下一篇介紹如何構(gòu)建列表,歡迎點(diǎn)贊鼓勵(lì)!

以上就是Flutter 如何設(shè)置App的主色調(diào)與字體的詳細(xì)內(nèi)容,更多關(guān)于Flutter 設(shè)置App的主色調(diào)與字體的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論