import 'package:flutter/material.dart'; import '../localization/localization.dart'; import '../router/custom_router.dart'; import '../router/route_constants.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import '../localization/language_constants.dart'; void main() => runApp(translation()); class translation extends StatefulWidget { const translation({Key key}) : super(key: key); static void setLocale(BuildContext context, Locale newLocale) { _MyAppState state = context.findAncestorStateOfType<_MyAppState>(); state.setLocale(newLocale); } @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State { Locale _locale; setLocale(Locale locale) { setState(() { _locale = locale; }); } @override void didChangeDependencies() { getLocale().then((locale) { setState(() { this._locale = locale; }); }); super.didChangeDependencies(); } @override Widget build(BuildContext context) { if (this._locale == null) { return Container( child: Center( child: CircularProgressIndicator( valueColor: AlwaysStoppedAnimation(Colors.blue[800])), ), ); } else { return MaterialApp( debugShowCheckedModeBanner: false, title: "Flutter Localization Demo", theme: ThemeData(primarySwatch: Colors.blue), locale: _locale, supportedLocales: [ Locale("en", "US"), Locale("fa", "IR"), Locale("es", "SP"), Locale("hi", "IN") ], localizationsDelegates: [ DemoLocalization.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], localeResolutionCallback: (locale, supportedLocales) { for (var supportedLocale in supportedLocales) { if (supportedLocale.languageCode == locale.languageCode && supportedLocale.countryCode == locale.countryCode) { return supportedLocale; } } return supportedLocales.first; }, onGenerateRoute: CustomRouter.generatedRoute, initialRoute: homeRoute, ); } } }