为 Flutter 应用程序定义通用主题的最佳方法是通过 MaterialApp 的 theme 属性。你可以这样做:
MaterialApp(
title: 'Sample App',
theme: ThemeData(
// Define the default brightness and colors.
brightness: Brightness.dark,
primaryColor: Colors.lightBlue[800],
accentColor: Colors.cyan[600],
// Define the default font family.
fontFamily: 'Georgia',
// Define the default TextTheme. Use this to specify the default
// text styling for headlines, titles, bodies of text, and more.
textTheme: const TextTheme(
bodyText2: TextStyle(fontSize: 22, fontFamilyFallback: ['AppleColorEmoji']), // Default style for Material Text
subtitle1: TextStyle(fontSize: 24, fontFamilyFallback: ['AppleColorEmoji']), // Define your own style of subtitle
),
),
home: Scaffold(
appBar: AppBar(title: Text('Sample title', style: Theme.of(context).textTheme.subtitle1)), // Use your default config like this
body: Text('Sample content'), // Without style, it will use your bodyText2 config
),
);
阅读更多关于 Flutter 的 Theme here
更新:要仅为您的应用全局设置后备字体,请在根小部件处使用 DefaultTextStyle。例如:
Scaffold(
body: DefaultTextStyle(
style: TextStyle(fontFamilyFallback: ['AppleColorEmoji']),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'The first text',
),
],
),
),
),
)
您可以在此处阅读更多关于Text documentation 的信息
样式参数是可选的。省略时,文本将使用最接近的封闭 DefaultTextStyle 中的样式。