✨中華フォント対応 - Ki-Kobayashi/flutter_wiki GitHub Wiki
- flutter_localization を使う方法
- google_fonts を使う方法
.
複数系の方をimportしないと、undifined となり、Delegete系が呼び出せない
import 'package:flutter_localizations/flutter_localizations.dart';
@override
Widget build(BuildContext context) {
final goRouter = ref.watch(routerProvider);
return MaterialApp.router(
scaffoldMessengerKey: rootScaffoldMessengerKey,
debugShowCheckedModeBanner: false, // debugバナーを非表示
// 👇以下から記述
localizationsDelegates: const[
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalMaterialLocalizations.delegate
],
supportedLocales: const [
Locale("ja", "JP"),
],
上記で日本語フォントになるとのこと
.
https://pub.dev/packages/google_fonts
.
参考:https://www.memory-lovers.blog/entry/2023/09/18/104233
GoogleFonts.config.allowRuntimeFetching = kDebugMode;
@override
Widget build(BuildContext context) {
final goRouter = ref.watch(routerProvider);
return MaterialApp.router(
scaffoldMessengerKey: rootScaffoldMessengerKey,
debugShowCheckedModeBanner: false,
routerConfig: goRouter,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: ColorConstants.appRed,
),
appBarTheme: const AppBarTheme(
backgroundColor: Colors.white,
shadowColor: Colors.transparent,
),
// 👇App全体の設定
textTheme: GoogleFonts.notoSansJpTextTheme(Theme.of(context).textTheme),
tabBarTheme: TabBarTheme(
labelColor: ColorConstants.labelColor,
dividerColor: ColorConstants.dividerColor,
unselectedLabelColor: ColorConstants.unselectedLabelColor,
indicatorColor: ColorConstants.indicatorColor,
// 👇 ラベルテキストのフォントデザイン
labelStyle: GoogleFonts.notoSansJp(
fontWeight: FontWeight.w700,
),
// 👇 選択されていないときのフォントデザイン
unselectedLabelStyle: GoogleFonts.notoSansJp(
fontWeight: FontWeight.w700,
),
),
bottomSheetTheme:
const BottomSheetThemeData(modalBackgroundColor: Colors.white),
// 👇 画面遷移のアニメーションをiphoneに揃える
pageTransitionsTheme: const PageTransitionsTheme(
builders: <TargetPlatform, PageTransitionsBuilder>{
TargetPlatform.android: CupertinoPageTransitionsBuilder(),
TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
},
),
// 👇マテリアルテーマを有効にしたい場合は以下
useMaterial3: true,
),
themeMode: ThemeMode.light,
);
}
.
TextWidgetなら、fontfamilyプロパティを使用する
Text(
'xxxx',
style: const TextStyle(
fontFamily: 'SF Pro'
),
)
.
基本、google_fontsは、ネット通信をしてフォント取得・表示をしている
不要なネット通信を減らすために、フォントデータはAppに内容されたものを使用したい。
ただし、開発中は様々なフォントを試したいニーズもあるので、開発中だけはネット通信を許可するようにする。
それが、下記の設定。(🚨併せて、Appに内包するフォントをダウンロードし、格納しておく)
.
FutureOr<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// 👇下の設定を入れると、Debugの時だけ、存在しないフォントをネットワークアクセス取得する(リリース版は無効になる)
// 🚨個の設定を入れる場合は、次項の設定 **必須**
GoogleFonts.config.allowRuntimeFetching = kDebugMode;
.
@override
Widget build(BuildContext context) {
final goRouterConfig = ref.watch(routerProvider);
return MaterialApp.router(
debugShowCheckedModeBanner: false,
scaffoldMessengerKey: rootScaffoldMessengerKey,
routerConfig: goRouterConfig,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: ColorConstants.appRed,
),
textTheme: GoogleFonts.notoSansJpTextTheme(Theme.of(context).textTheme),
appBarTheme: const AppBarTheme(
backgroundColor: Colors.white,
shadowColor: Colors.transparent,
),
tabBarTheme: TabBarTheme(
labelColor: Colors.orange,
dividerColor: Colors.darkGrey,
unselectedLabelColor: Colors.darkGrey,
indicatorColor: Colors.orange,
labelStyle: GoogleFonts.notoSansJp(
fontWeight: FontWeight.w500,
),
unselectedLabelStyle: GoogleFonts.notoSansJp(
fontWeight: FontWeight.w400,
),
),
pageTransitionsTheme: const PageTransitionsTheme(
builders: <TargetPlatform, PageTransitionsBuilder>{
TargetPlatform.android: CupertinoPageTransitionsBuilder(),
TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
},
),
bottomSheetTheme:
const BottomSheetThemeData(modalBackgroundColor: Colors.white),
),
themeMode: ThemeMode.light,
);
}
.
参考:https://www.memory-lovers.blog/entry/2023/09/18/104233
. google_fontsはネットワーク経由でフォントファイルを取得してくれる。 開発中はよいけど、本番時はアプリに含めてリリースしたい。
. フォントファイルをassetsのgoogle_fontsというフォルダを用意しておくと、 自動的にそちらを使ってくれる機能があるらしい。
. Bundling fonts when releasing | google_fonts | Flutter Package
.
- https://fonts.google.com/ からフォントをダウンロード
- zip を展開して static/配下 の ttfファイル をコピー
- assets/google_fonts/ フォルダを作成して、コピーした ttfファイル を配置
- pubspec.yaml に assets/google_fonts/ を追加
# assets/google_fontsの中身
assets/google_fonts/
├── NotoSansJP-Black.ttf
├── NotoSansJP-Bold.ttf
├── NotoSansJP-ExtraBold.ttf
├── NotoSansJP-ExtraLight.ttf
├── NotoSansJP-Light.ttf
├── NotoSansJP-Medium.ttf
├── NotoSansJP-Regular.ttf
├── NotoSansJP-SemiBold.ttf
└── NotoSansJP-Thin.ttf
.
### pubspec.yaml
# アセットとして認識されるようにフォルダを追加
assets:
- assets/google_fonts/
.
.
.
.