Skip to content

Commit 81357fe

Browse files
committed
Refactor language switcher to use PopupMenuButton for improved UI and locale selection
1 parent 1247c2a commit 81357fe

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

lib/app/widgets/language_switcher.dart

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,54 @@ class LanguageSwitcher extends StatelessWidget {
77

88
@override
99
Widget build(BuildContext context) {
10-
return Row(
11-
mainAxisSize: MainAxisSize.min,
12-
children: [
13-
ElevatedButton(
14-
onPressed: () {
15-
context.read<LocalizationProvider>().setLocale('en');
16-
},
17-
child: const Text('English'),
10+
final currentLocale = context.watch<LocalizationProvider>().locale;
11+
final currentLanguage = currentLocale.languageCode == 'en' ? 'English' : 'ไทย';
12+
13+
return PopupMenuButton<String>(
14+
icon: Row(
15+
mainAxisSize: MainAxisSize.min,
16+
children: [
17+
Text(
18+
currentLanguage,
19+
style: const TextStyle(
20+
color: Colors.black87,
21+
fontWeight: FontWeight.w500,
22+
),
23+
),
24+
const Icon(Icons.arrow_drop_down, color: Colors.black87),
25+
],
26+
),
27+
itemBuilder: (context) => [
28+
PopupMenuItem(
29+
value: 'en',
30+
child: Row(
31+
children: [
32+
const Text('English'),
33+
if (currentLocale.languageCode == 'en')
34+
const Padding(
35+
padding: EdgeInsets.only(left: 8.0),
36+
child: Icon(Icons.check, size: 18),
37+
),
38+
],
39+
),
1840
),
19-
const SizedBox(width: 8),
20-
ElevatedButton(
21-
onPressed: () {
22-
context.read<LocalizationProvider>().setLocale('th');
23-
},
24-
child: const Text('ไทย'),
41+
PopupMenuItem(
42+
value: 'th',
43+
child: Row(
44+
children: [
45+
const Text('ไทย'),
46+
if (currentLocale.languageCode == 'th')
47+
const Padding(
48+
padding: EdgeInsets.only(left: 8.0),
49+
child: Icon(Icons.check, size: 18),
50+
),
51+
],
52+
),
2553
),
2654
],
55+
onSelected: (String languageCode) {
56+
context.read<LocalizationProvider>().setLocale(languageCode);
57+
},
2758
);
2859
}
2960
}

0 commit comments

Comments
 (0)