Как установить letterSpacing для всего приложения во Flutter?
Я работаю с Flutter и знаю, что свойство letterSpacing полезно для задания некоторого расстояния между буквами.
Я хочу дать это всему приложению, я имею в виду, где бы я ни писал какой-либо текст в приложении. Я хочу установить интервал между буквами 1.0 для всего текста.
Есть ли способ это сделать?
2 ответа
Вы можете определить TextTheme и установить собственный TextStyle для каждого типа текста (заголовок1, ...).
Если тема не указана, Flutter определит для вас тему по умолчанию.
Однако вы можете переопределить или расширить значение по умолчанию и определить свойство letterSpacing. Я показываю пример для заголовка1 и кнопки ниже.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
textTheme: TextTheme(
headline1: Theme.of(context)
.textTheme
.bodyText1
.copyWith(letterSpacing: 1.0),
button: Theme.of(context)
.textTheme
.button
.copyWith(letterSpacing: 1.0),
),
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
Вы можете определить const DEF_TEXT_STYLE в файле constants.dart, а затем применять его, когда захотите.
constants.dart
const DEF_TEXT_STYLE = const TextStyle(
letterSpacing: 1.0);
Подать заявку можно было так:
Text(
'This is my text',
style: DEF_TEXT_STYLE,
),
Не забудьте импортировать файл constants.dart.
В противном случае вы можете перезаписать все данные textTheme, аналогично тому, что сказал @glavigno:
Здесь вы можете увидеть все данные textTheme, доступные во флаттере. ДОКУМЕНТЫ
theme: ThemeData(
textTheme: TextTheme(
headline1: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
headline2: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
headline3: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
headline4: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
headline5: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
headline6: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
subtitle1: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
subtitle2: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
bodyText1: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
bodyText2: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
button: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
caption: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
overline: Theme.of(context)
.textTheme
.headline1
.copyWith(letterSpacing: 1.0),
),
),