Skip to main content

現場で使える Flutter開発入門の8章を読んで不明点を調べた


現場で使える Flutter開発入門の6章と7章を読んで不明点を調べた
の続編

調べたことを備忘録で記載する。


・ローカライズ
jsonファイルを読み込んでそれを表示する。

・LocalizationsDelegate
shouldReload method
>This method is called whenever its Localizations widget is rebuilt. If it returns true then dependent widgets will be rebuilt after load has completed.
https://api.flutter.dev/flutter/widgets/LocalizationsDelegate/shouldReload.html

load method
>It’s assumed that the this method will return an object that contains a collection of related resources (typically defined with one method per resource). The object will be retrieved with Localizations.of.
https://api.flutter.dev/flutter/widgets/LocalizationsDelegate/load.html

isSupported
>Whether resources for the given locale can be loaded by this delegate.
https://api.flutter.dev/flutter/widgets/LocalizationsDelegate/isSupported.html

・アプリ文言以外のローカライズ
まずパッケージを入れる。
package:flutter_localizations/flutter_localizations.dart
https://qiita.com/maria_mari/items/4b2780d5657581c4f406

次にdelegateをmain.dartに書く。
>GlobalMaterialLocalizations.delegate は多分組み込みのローカライズのもの?日付のフォーマットなどが管理されている。
>GlobalWidgetsLocalizationsはウィジェットをローカライズするために必要?右から書く言語とかに対応するためにいるのか?
https://qiita.com/sekitaka_1214/items/99a128e2ee7463849232
GlobalCupertinoLocalizations
>Flutterで多言語対応をした場合にiosで言語を日本語にして、TextFieldで長押しするとクラッシュします。
>原因は、長押ししたときに表示されるポップアップメニューに対応するラベルがないためです。
https://qiita.com/sh-ogawa/items/94d560d0419433bf1e75

・localeListResolutionCallback
本の内容動かないので直した。

localeListResolutionCallback: (locales, supportedLocales) {
        if (locales == null) {
          return supportedLocales.first;
        }

        for (var supportedLocale in supportedLocales) {
          return locales.firstWhere((element) =>
              supportedLocale.languageCode == element.languageCode &&
              supportedLocale.countryCode == element.countryCode);
        }
        return supportedLocales.first;

 

・firstWhereOrNull
追加パッケージを入れるとコレクションの関数が増える。
> collection: ^1.15.0
> import ‘package:collection/collection.dart’;
https://qiita.com/escargot9101/items/9602e756cb9c6c5ce931

 

・Cupertino UI
iOSのUIを表現するパッケージ。
https://qiita.com/nagahama/items/fe3b6ba210f9bfdb6408

 

関連記事:

Pocket