1
0
mirror of https://github.com/h7x4/Jisho-Study-Tool.git synced 2024-12-23 06:17:29 +01:00
Jisho-Study-Tool/lib/view/components/search/language_selector.dart

61 lines
1.7 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
2021-12-01 23:09:53 +01:00
import '../../../models/themes/theme.dart';
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();
Widget _languageOption(String language) =>
Container(
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Center(child: Text(language)),
);
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,
2021-12-06 20:26:52 +01:00
children: <Widget>[
_languageOption('Auto'),
_languageOption('日本語'),
_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
);
}
}