39 lines
1.3 KiB
Dart
39 lines
1.3 KiB
Dart
import 'package:flutter/material.dart';
|
|||
|
|
import 'package:provider/provider.dart';
|
||
|
|
import '../providers/locale_provider.dart';
|
||
|
|
|
||
|
|
/// One-tap language switch driving ARB + dynamic strings together (Group-2).
|
||
|
|
/// Drop into profile / settings / app bar:
|
||
|
|
/// const LanguageSwitcher()
|
||
|
|
class LanguageSwitcher extends StatelessWidget {
|
||
|
|
final bool compact;
|
||
|
|
const LanguageSwitcher({super.key, this.compact = false});
|
||
|
|
|
||
|
|
static const _labels = {'en': 'English', 'hi': 'हिन्दी'};
|
||
|
|
|
||
|
|
@override
|
||
|
|
Widget build(BuildContext context) {
|
||
|
|
final provider = Provider.of<LocaleProvider>(context);
|
||
|
|
if (compact) {
|
||
|
|
return DropdownButtonHideUnderline(
|
||
|
|
child: DropdownButton<String>(
|
||
|
|
value: provider.code,
|
||
|
|
items: LocaleProvider.supportedCodes
|
||
|
|
.map((c) => DropdownMenuItem(value: c, child: Text(_labels[c] ?? c)))
|
||
|
|
.toList(),
|
||
|
|
onChanged: (c) {
|
||
|
|
if (c != null) provider.setLocale(c);
|
||
|
|
},
|
||
|
|
),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return SegmentedButton<String>(
|
||
|
|
segments: LocaleProvider.supportedCodes
|
||
|
|
.map((c) => ButtonSegment(value: c, label: Text(_labels[c] ?? c)))
|
||
|
|
.toList(),
|
||
|
|
selected: {provider.code},
|
||
|
|
onSelectionChanged: (s) => provider.setLocale(s.first),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|