`
import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:flutter_i18n/flutter_i18n_delegate.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
localizationsDelegates: [
FlutterI18nDelegate(false, 'en', 'assets/i18n'),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
);
}
}
class MyHomePage extends StatefulWidget {
@OverRide
MyHomeState createState() => new MyHomeState();
}
class MyHomeState extends State {
String currentLang = 'en';
int clicked = 0;
changeLanguage() {
setState(() {
currentLang = currentLang == 'en' ? 'it' : 'en';
});
}
incrementCounter() {
setState(() {
clicked++;
});
}
@OverRide
Widget build(BuildContext context) {
return new Scaffold(
appBar:
new AppBar(title: new Text(FlutterI18n.translate(context, "title"))),
body: new Builder(builder: (BuildContext context) {
return new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
new Text(FlutterI18n.translate(context, "label.main",
Map.fromIterables(["user"], ["Flutter lover"]))),
new Text(FlutterI18n.plural(context, "clicked.times", clicked)),
new FlatButton(
onPressed: () async {
incrementCounter();
},
child: new Text(
FlutterI18n.translate(context, "button.clickMe"))),
new FlatButton(
onPressed: () async {
changeLanguage();
await FlutterI18n.refresh(context, currentLang);
Scaffold.of(context).showSnackBar(new SnackBar(
content: new Text(
FlutterI18n.translate(context, "toastMessage")),
));
},
child: new Text(
FlutterI18n.translate(context, "button.clickMe")))
],
),
);
}),
);
}
}
`
Take this example. When I click the button to change the language to italian everything works fine. But if I rotate screen the language changes back to English.