1. 程式人生 > 其它 >短視訊系統原始碼,Flutter 設定 App 的主色調與字型

短視訊系統原始碼,Flutter 設定 App 的主色調與字型

短視訊系統原始碼,Flutter 設定 App 的主色調與字型實現的相關程式碼

Flutter 的主題色和字型可以在MaterialApp 中設定,即在 main.dart 的入口返回的 MaterialApp 元件統一設定全域性的主色調和字型。如下面的程式碼所示:

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

​通過 MateriaApp 的 theme 屬性,構建 ThemeData 來配置全域性主題。其中ThemeData常用的屬性如下所示:

brightness:為 Brightness 列舉,包括 dark 和 light 兩種模式,其中 dark 對應的是深色模式(即夜間模式),light 對應淺色模式。

primaryColor:主色調,設定後導航欄就會變成主色調顏色。注意的是導航欄的字型顏色會根據主色調和 brightness 自動計算顯示的顏色是偏淺色還是深色。

accentColor:輔助色,根據需要設定。

textTheme:文字主體。早期版本的 flutter 設定的比較少,新版本可能是為了支援Web端,字型的屬性設定基本和 html 的保持一致了,包括 headline1到 headline6,bodyText1,感覺就是對應 html 中的 h1-h6和 body 的字型。各級字型均可以通過構建 TextStyle 來設定對應的字型引數。

fontFamily:字型族。

在應用中可以通過 Theme.of(context)獲取當前主體,再獲取對應的屬性來繼承主題的色調或字型。如下面的程式碼的 Text 的樣式就繼承了主體的bodyText1的字型特性。

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

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

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('島上碼農', 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(
'動態', 'images/dynamic.png', 'images/dynamic-hover.png'),
_getBottomNavItem(
' 訊息', 'images/message.png', 'images/message-hover.png'),
_getBottomNavItem(
'分類瀏覽', 'images/category.png', 'images/category-hover.png'),
_getBottomNavItem(
'個人中心', 'images/mine.png', 'images/mine-hover.png'),
],
),
);
}

以上就是 短視訊系統原始碼,Flutter 設定 App 的主色調與字型實現的相關程式碼,更多內容歡迎關注之後的文章