forked from ilteoood/flutter_i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.dart
69 lines (63 loc) · 2.01 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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'),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
);
}
}
class MyHomePage extends StatefulWidget {
@override
MyHomeState createState() => new MyHomeState();
}
class MyHomeState extends State<MyHomePage> {
String currentLang = 'en';
switchLang() {
setState(() {
currentLang = currentLang == 'en' ? 'it' : 'en';
});
}
@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: <Widget>[
new Text(FlutterI18n.translate(context, "label.main",
Map.fromIterables(["user"], ["Flutter lover"]))),
new FlatButton(
onPressed: () async {
switchLang();
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")))
],
),
);
}),
);
}
}