Jisho-Study-Tool/lib/view/components/search/LanguageSelector.dart

76 lines
1.8 KiB
Dart
Raw Normal View History

2021-03-02 22:26:59 +01:00
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
2021-03-02 22:26:59 +01:00
class LanguageSelector extends StatefulWidget {
@override
2021-03-03 00:24:25 +01:00
_LanguageSelectorState createState() => new _LanguageSelectorState();
2021-03-02 22:26:59 +01:00
}
2021-03-03 00:24:25 +01:00
class _LanguageSelectorState extends State<LanguageSelector> {
SharedPreferences prefs;
2021-03-02 23:35:47 +01:00
List<bool> isSelected;
2021-03-02 22:26:59 +01:00
@override
void initState() {
super.initState();
isSelected = [false, false, false];
SharedPreferences.getInstance()
.then((prefs) {
this.prefs = prefs;
setState(() {
isSelected = _getSelectedStatus() ?? isSelected;
});
});
}
void _updateSelectedStatus() async {
await prefs.setStringList('languageSelectorStatus',
isSelected
.map((b) => b ? '1' : '0')
.toList());
}
List<bool> _getSelectedStatus() {
return prefs
.getStringList('languageSelectorStatus')
?.map((s) => s == '1')
?.toList();
2021-03-02 22:26:59 +01:00
}
@override
Widget build(BuildContext context) {
return ToggleButtons(
2021-03-02 23:35:47 +01:00
isSelected: isSelected,
children: <Widget> [
2021-03-03 00:24:25 +01:00
_LanguageOption("Auto"),
_LanguageOption("Japanese"),
_LanguageOption("English")
2021-03-02 22:26:59 +01:00
],
2021-03-02 23:35:47 +01:00
selectedColor: Colors.blue,
onPressed: (int buttonIndex) {
setState(() {
for (var i in Iterable.generate(isSelected.length)) {
isSelected[i] = i == buttonIndex;
}
_updateSelectedStatus();
2021-03-02 23:35:47 +01:00
});
},
2021-03-02 22:26:59 +01:00
);
}
}
2021-03-03 00:24:25 +01:00
class _LanguageOption extends StatelessWidget {
final String language;
2021-03-02 22:26:59 +01:00
2021-03-03 00:24:25 +01:00
_LanguageOption(this.language);
2021-03-02 22:26:59 +01:00
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: Center(child: Text(language)),
2021-03-02 22:26:59 +01:00
);
}
}