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

82 lines
2.7 KiB
Dart
Raw Normal View History

2022-09-24 09:36:22 +02:00
import 'package:antd_mobile/antd_mobile.dart';
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 {
2022-09-24 07:22:07 +02:00
const MyApp({super.key});
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
};
2022-09-24 20:09:46 +02:00
return AntTheme(
2021-02-14 06:01:09 +01:00
key: auth.rootKey,
2022-09-24 20:09:46 +02:00
data: AntThemeData(brightness: theme.brightness),
child: Builder(
builder: (context) {
final antTheme = AntTheme.of(context);
return CupertinoApp.router(
routeInformationProvider: router.routeInformationProvider,
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
theme: CupertinoThemeData(
brightness: theme.brightness,
primaryColor: antTheme.colorPrimary,
2022-10-04 14:18:04 +02:00
scaffoldBackgroundColor: antTheme.colorBox,
2022-09-24 20:09:46 +02:00
textTheme: CupertinoTextThemeData(
textStyle: TextStyle(
fontSize: antTheme.fontSizeMain,
color: antTheme.colorText,
),
),
),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
localeListResolutionCallback: localeListResolutionCallback,
);
},
2022-09-17 14:35:45 +02:00
),
2021-02-14 06:01:09 +01:00
);
2020-02-01 07:44:15 +01:00
}
2020-01-14 11:13:07 +01:00
}