git-touch-android-ios-app/lib/app.dart

62 lines
2.0 KiB
Dart
Raw Normal View History

2020-01-14 11:13:07 +01:00
import 'package:flutter/cupertino.dart';
2022-09-11 19:54:07 +02:00
import 'package:flutter_gen/gen_l10n/S.dart';
2020-02-01 07:44:15 +01:00
import 'package:git_touch/models/auth.dart';
2020-01-14 11:13:07 +01:00
import 'package:git_touch/models/theme.dart';
2022-09-11 19:54:07 +02:00
import 'package:git_touch/router.dart';
2021-02-27 04:42:11 +01:00
import 'package:intl/locale.dart' as l;
2022-09-11 19:54:07 +02:00
import 'package:provider/provider.dart';
2020-01-14 11:13:07 +01:00
class MyApp extends StatelessWidget {
2020-02-01 07:44:15 +01:00
@override
Widget build(BuildContext context) {
final auth = Provider.of<AuthModel>(context);
2021-02-14 06:01:09 +01:00
final theme = Provider.of<ThemeModel>(context);
2022-09-06 18:32:56 +02:00
// ignore: prefer_function_declarations_over_variables
2021-02-14 06:01:09 +01:00
final LocaleListResolutionCallback localeListResolutionCallback =
(locales, supportedLocales) {
2021-02-27 04:42:11 +01:00
// 1. user set locale
// 2. system locale
try {
if (theme.locale != null) {
2021-05-16 09:16:35 +02:00
final intlLocale = l.Locale.parse(theme.locale!);
2021-02-27 04:42:11 +01:00
locales = [
Locale.fromSubtags(
languageCode: intlLocale.languageCode,
countryCode: intlLocale.countryCode,
scriptCode: intlLocale.scriptCode,
),
2021-05-16 09:16:35 +02:00
...locales!
2021-02-27 04:42:11 +01:00
];
}
} catch (err) {
print(err);
}
2021-05-16 09:16:35 +02:00
for (final locale in locales!) {
2021-02-27 04:42:11 +01:00
// this is necessary because Flutter only handles zh_Hans -> zh
// and would not handle non-exist language code
if (AppLocalizations.delegate.isSupported(locale)) {
return locale;
}
}
// 3. if none match, use the default
return supportedLocales.firstWhere((l) => l.languageCode == 'en');
2021-02-14 06:01:09 +01:00
};
return Container(
key: auth.rootKey,
2022-09-17 14:35:45 +02:00
child: CupertinoApp.router(
routeInformationProvider: router.routeInformationProvider,
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
theme: CupertinoThemeData(brightness: theme.brightness),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
localeListResolutionCallback: localeListResolutionCallback,
),
2021-02-14 06:01:09 +01:00
);
2020-02-01 07:44:15 +01:00
}
2020-01-14 11:13:07 +01:00
}