2021-03-02 22:26:59 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2021-03-05 00:38:35 +01:00
|
|
|
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> {
|
2021-07-26 21:39:17 +02:00
|
|
|
late final SharedPreferences prefs;
|
|
|
|
late List<bool> isSelected;
|
2021-03-02 22:26:59 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2021-03-05 00:38:35 +01:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:39:17 +02:00
|
|
|
List<bool>? _getSelectedStatus() {
|
2021-03-05 00:38:35 +01:00
|
|
|
return prefs
|
|
|
|
.getStringList('languageSelectorStatus')
|
|
|
|
?.map((s) => s == '1')
|
2021-07-26 21:39:17 +02:00
|
|
|
.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;
|
|
|
|
}
|
2021-03-05 00:38:35 +01:00
|
|
|
_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) {
|
2021-03-05 00:38:35 +01:00
|
|
|
return Container(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
|
|
|
|
child: Center(child: Text(language)),
|
2021-03-02 22:26:59 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|