/
main_with_dynamic_options.dart
315 lines (295 loc) · 11.2 KB
/
main_with_dynamic_options.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import 'package:animated_toggle_switch/animated_toggle_switch.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:locale_switcher/locale_switcher.dart';
import 'package:url_launcher/url_launcher.dart';
/// Useful extension, in case you need to use localization outside of MaterialApp.
extension LocaleWithDelegate on Locale {
/// Get class with translation strings for this locale.
AppLocalizations get tr => lookupAppLocalizations(this);
}
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
// ============= THIS 4 LINES ARE REQUIRED =============
return LocaleManager(
child: MaterialApp(
locale: LocaleSwitcher.localeBestMatch,
supportedLocales: AppLocalizations.supportedLocales,
// ...
localizationsDelegates: AppLocalizations.localizationsDelegates,
title: LocaleSwitcher.locale.value.tr.example,
home: MyHomePage(title: LocaleSwitcher.locale.value.tr.example),
debugShowCheckedModeBanner: false,
// themeMode: ThemeMode.dark,
theme: ThemeData
// .dark().copyWith
(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.lightBlue[400]!),
useMaterial3: true,
textTheme: const TextTheme(bodyMedium: TextStyle(fontSize: 18)),
),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int showNletters = 0;
bool showLeading = true;
bool circleOrSquare = true;
bool useEmoji = false;
double size = 1;
@override
Widget build(BuildContext context) {
final loc = AppLocalizations.of(context); // localization shortcut
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.primaryContainer,
title: Text(widget.title),
actions: [
// =============== THIS LINE ===============
LocaleSwitcher.iconButton(
useNLettersInsteadOfIcon: showNletters,
useEmoji: useEmoji,
shape: circleOrSquare ? const CircleBorder(eccentricity: 0) : null,
),
],
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.fromLTRB(10, 30, 20, 10),
child: LayoutBuilder(
builder: (context, constrains) {
final children = [
Expanded(
child: Center(
child: Column(
children: [
LocaleSwitcher.menu(
width: 310,
iconRadius: 38 * size,
title: loc.chooseLanguage,
useNLettersInsteadOfIcon: showNletters,
useEmoji: useEmoji,
showLeading: showLeading,
shape: circleOrSquare
? const CircleBorder(eccentricity: 0)
: null,
),
Divider(
color: Theme.of(context).canvasColor,
height: 20,
),
SizedBox(
width: 400,
// =============== THIS LINE ===============
child: LocaleSwitcher.custom(
numberOfShown: 2,
builder: (langCodes, context) {
if (langCodes.length <= 1) {
// AnimatedToggleSwitch crash with one value
langCodes.addShowOtherLocales();
}
return AnimatedToggleSwitch<LocaleName>.rolling(
values: langCodes,
height: 48 * size,
indicatorSize: Size(48 * size, 48 * size),
fittingMode: FittingMode.none,
current: LocaleSwitcher.current,
onChanged: (langCode) {
if (langCode.name == showOtherLocales) {
showSelectLocaleDialog(context);
} else {
LocaleSwitcher.current = langCode;
}
},
iconBuilder: (lang, foreground) =>
LangIconWithToolTip(
localeNameFlag: lang,
radius: 48 * size,
useNLettersInsteadOfIcon: showNletters,
),
allowUnlistedValues: true,
loading: false,
style: ToggleStyle(
backgroundColor: Colors.black12,
indicatorColor: Theme.of(context)
.colorScheme
.primaryContainer,
),
);
},
// shape: circleOrSquare? const CircleBorder(eccentricity: 0) : null,
),
),
TitleForLocaleSwitch(
title: loc.chooseLanguage,
childSize: Size(400, 48 * size),
child: LocaleSwitcher.segmentedButton(
iconRadius: 32 * size,
// width: 400,
useNLettersInsteadOfIcon: showNletters,
useEmoji: useEmoji,
numberOfShown: 2,
shape: circleOrSquare
? const CircleBorder(eccentricity: 0)
: null,
),
),
const CounterWidget(),
Center(
child: RichText(
text: TextSpan(
children: [
const TextSpan(
text: 'Source code of this page: ',
style: TextStyle(color: Colors.black),
),
TextSpan(
text: 'here',
style: const TextStyle(color: Colors.blue),
recognizer: TapGestureRecognizer()
..onTap = () {
launchUrl(
Uri.https(
'github.com',
'/Alexqwesa/locale_switcher/blob/main/example/lib/main_with_dynamic_options.dart',
),
);
},
),
],
),
),
),
],
),
),
),
Column(
children: [
const SizedBox(
width: 220,
),
Text(loc.showIcons),
Switch(
value: showLeading,
onChanged: (val) {
setState(() {
showLeading = val;
});
},
),
Text(loc.showLetters),
Switch(
key: const ValueKey('letterSwitch'),
value: (showNletters != 0),
onChanged: (val) {
setState(() {
if (!val) {
showNletters = 0;
} else {
showNletters = 2;
useEmoji = false;
}
});
},
),
Text(loc.circleOrSquare),
Switch(
value: !circleOrSquare,
onChanged: (val) {
setState(() {
circleOrSquare = !val;
showNletters = 0;
useEmoji = false;
});
},
),
Text(loc.showEmoji),
Switch(
value: useEmoji,
onChanged: (val) {
setState(() {
useEmoji = val;
showNletters = 0;
});
},
),
Slider(
min: 0.2,
max: 2,
value: size,
onChanged: (val) {
setState(() {
size = val;
});
}),
],
)
];
final rc = (constrains.maxWidth < 800);
return rc
? SizedBox(height: 800, child: Column(children: children))
: Row(children: children);
},
),
),
),
);
}
}
///
/// BELOW IS JUST SOME RANDOM CODE - YOU DON'T NEED IT!!!!!!
///
class CounterWidget extends StatefulWidget {
const CounterWidget({super.key});
@override
State<CounterWidget> createState() => _CounterWidgetState();
}
class _CounterWidgetState extends State<CounterWidget> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
final loc = AppLocalizations.of(context);
return Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
const SizedBox(
height: 20,
),
Text(
loc.counterDescription,
textAlign: TextAlign.center,
softWrap: false,
overflow: TextOverflow.fade,
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
FloatingActionButton(
onPressed: _incrementCounter,
tooltip: loc.increment,
child: const Icon(Icons.add),
),
],
),
);
}
}