Ei kuvausta

main.dart 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import 'package:flutter/material.dart';
  2. import 'package:userstory2translate/localization/localization.dart';
  3. import 'package:userstory2translate/router/custom_router.dart';
  4. import 'package:userstory2translate/router/route_constants.dart';
  5. import 'package:flutter_localizations/flutter_localizations.dart';
  6. import 'localization/language_constants.dart';
  7. void main() => runApp(MyApp());
  8. class MyApp extends StatefulWidget {
  9. const MyApp({Key key}) : super(key: key);
  10. static void setLocale(BuildContext context, Locale newLocale) {
  11. _MyAppState state = context.findAncestorStateOfType<_MyAppState>();
  12. state.setLocale(newLocale);
  13. }
  14. @override
  15. _MyAppState createState() => _MyAppState();
  16. }
  17. class _MyAppState extends State<MyApp> {
  18. Locale _locale;
  19. setLocale(Locale locale) {
  20. setState(() {
  21. _locale = locale;
  22. });
  23. }
  24. @override
  25. void didChangeDependencies() {
  26. getLocale().then((locale) {
  27. setState(() {
  28. this._locale = locale;
  29. });
  30. });
  31. super.didChangeDependencies();
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. if (this._locale == null) {
  36. return Container(
  37. child: Center(
  38. child: CircularProgressIndicator(
  39. valueColor: AlwaysStoppedAnimation<Color>(Colors.blue[800])),
  40. ),
  41. );
  42. } else {
  43. return MaterialApp(
  44. debugShowCheckedModeBanner: false,
  45. title: "Flutter Localization Demo",
  46. theme: ThemeData(primarySwatch: Colors.blue),
  47. locale: _locale,
  48. supportedLocales: [
  49. Locale("en", "US"),
  50. Locale("fa", "IR"),
  51. Locale("es", "SP"),
  52. Locale("hi", "IN")
  53. ],
  54. localizationsDelegates: [
  55. DemoLocalization.delegate,
  56. GlobalMaterialLocalizations.delegate,
  57. GlobalWidgetsLocalizations.delegate,
  58. GlobalCupertinoLocalizations.delegate,
  59. ],
  60. localeResolutionCallback: (locale, supportedLocales) {
  61. for (var supportedLocale in supportedLocales) {
  62. if (supportedLocale.languageCode == locale.languageCode &&
  63. supportedLocale.countryCode == locale.countryCode) {
  64. return supportedLocale;
  65. }
  66. }
  67. return supportedLocales.first;
  68. },
  69. onGenerateRoute: CustomRouter.generatedRoute,
  70. initialRoute: homeRoute,
  71. );
  72. }
  73. }
  74. }