Skip to content

Instantly share code, notes, and snippets.

@podcoder
Last active April 1, 2020 08:05
Show Gist options
  • Save podcoder/124826a205fe817637ed85619a4621f2 to your computer and use it in GitHub Desktop.
Save podcoder/124826a205fe817637ed85619a4621f2 to your computer and use it in GitHub Desktop.
main page
import 'package:flutter/material.dart';
import 'package:flutter_localization_master/localization/demo_localization.dart';
import 'package:flutter_localization_master/router/custom_router.dart';
import 'package:flutter_localization_master/router/route_constants.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'localization/language_constants.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
const MyApp({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<MyApp> {
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<Color>(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("ar", "SA"),
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,
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment