Jisho-Study-Tool/lib/components/search/language_selector.dart

63 lines
1.8 KiB
Dart
Raw Normal View History

2021-03-02 22:26:59 +01:00
import 'package:flutter/material.dart';
2021-12-06 20:26:52 +01:00
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';
2021-03-02 22:26:59 +01:00
2022-01-19 02:10:05 +01:00
import '../../models/themes/theme.dart';
2022-01-23 23:56:26 +01:00
import '../../settings.dart';
2021-12-01 23:09:53 +01:00
2021-03-02 22:26:59 +01:00
class LanguageSelector extends StatefulWidget {
2021-12-01 23:09:53 +01:00
const LanguageSelector({Key? key}) : super(key: key);
2021-03-02 22:26:59 +01:00
@override
2021-12-01 23:09:53 +01:00
_LanguageSelectorState createState() => _LanguageSelectorState();
2021-03-02 22:26:59 +01:00
}
2021-03-03 00:24:25 +01:00
class _LanguageSelectorState extends State<LanguageSelector> {
2021-12-06 20:26:52 +01:00
final SharedPreferences prefs = GetIt.instance.get<SharedPreferences>();
2021-07-26 21:39:17 +02:00
late List<bool> isSelected;
2021-03-02 22:26:59 +01:00
@override
void initState() {
super.initState();
2021-12-06 20:26:52 +01:00
isSelected = _getSelectedStatus() ?? [false, false, false];
}
2021-12-06 20:26:52 +01:00
Future<void> _updateSelectedStatus() async => prefs.setStringList(
'languageSelectorStatus',
isSelected.map((b) => b ? '1' : '0').toList(),
);
2021-12-06 20:26:52 +01:00
List<bool>? _getSelectedStatus() => prefs
.getStringList('languageSelectorStatus')
?.map((s) => s == '1')
.toList();
2022-01-23 23:56:26 +01:00
Widget _languageOption(String language, {TextStyle? style}) =>
2021-12-06 20:26:52 +01:00
Container(
alignment: Alignment.center,
2021-12-06 20:26:52 +01:00
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
2022-01-23 23:56:26 +01:00
child: Text(language, style: style,),
2021-12-06 20:26:52 +01:00
);
2021-03-02 22:26:59 +01:00
@override
Widget build(BuildContext context) {
return ToggleButtons(
2021-08-08 23:16:54 +02:00
selectedColor: AppTheme.jishoGreen.background,
2021-03-02 23:35:47 +01:00
isSelected: isSelected,
2022-01-23 23:56:26 +01:00
children: [
2021-12-06 20:26:52 +01:00
_languageOption('Auto'),
2022-01-23 23:56:26 +01:00
_languageOption('日本語', style: japaneseFont.textStyle),
2021-12-06 20:26:52 +01:00
_languageOption('English')
2021-03-02 22:26:59 +01:00
],
2021-12-01 23:09:53 +01:00
onPressed: (buttonIndex) {
2021-03-02 23:35:47 +01:00
setState(() {
2021-12-01 23:09:53 +01:00
for (final int i in Iterable.generate(isSelected.length)) {
2021-03-02 23:35:47 +01:00
isSelected[i] = i == buttonIndex;
}
_updateSelectedStatus();
2021-03-02 23:35:47 +01:00
});
},
2021-03-02 22:26:59 +01:00
);
}
}