213 lines
6.0 KiB
Dart
213 lines
6.0 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:checker/base/base_screen.dart';
|
||
import 'package:checker/base/base_state.dart';
|
||
import 'package:checker/common.dart';
|
||
import 'package:checker/consts.dart';
|
||
import 'package:checker/db.dart';
|
||
import 'package:checker/network.dart';
|
||
import 'package:checker/screens/currencies.dart';
|
||
import 'package:checker/screens/languages.dart';
|
||
import 'package:checker/screens/splash.dart';
|
||
import 'package:checker/strings.dart';
|
||
import 'package:flutter/material.dart';
|
||
|
||
class SettingsScreen extends BaseScreen {
|
||
|
||
final bool returnToScanner;
|
||
|
||
SettingsScreen(helper, app, this.returnToScanner) : super(helper, app);
|
||
|
||
@override State createState() =>
|
||
new SettingsState(helper, app, returnToScanner);
|
||
}
|
||
|
||
class MenuItem {
|
||
|
||
// Заголовок пункта меню и выбранное значение.
|
||
String title, selectedValue;
|
||
|
||
MenuItem(this.title, this.selectedValue);
|
||
}
|
||
|
||
class SettingsState extends BaseState<SettingsScreen> {
|
||
|
||
List<MenuItem> menuItems = [
|
||
new MenuItem('', ''),
|
||
new MenuItem('', ''),
|
||
new MenuItem('', '')
|
||
];
|
||
|
||
bool returnToScanner;
|
||
String versionName = "";
|
||
|
||
bool isAutomaticallyImplyLeading() => true;
|
||
|
||
SettingsState(SqliteHelper helper, String app, bool returnToScanner) : super(helper, app) {
|
||
this.returnToScanner = returnToScanner;
|
||
}
|
||
|
||
@override Widget build(BuildContext ctx) {
|
||
if (this.versionName == "") {
|
||
platform.invokeMethod('getVersionName').then((versionName) {
|
||
setState(() {
|
||
this.versionName = versionName;
|
||
});
|
||
});
|
||
}
|
||
// TODO: Перенести в initStater
|
||
helper.getSettings(true).then((info) {
|
||
setState(() {
|
||
menuItems[0].title = StringsLocalization.currency();
|
||
menuItems[1].title = StringsLocalization.locale();
|
||
menuItems[2].title = StringsLocalization.logout();
|
||
menuItems[0].selectedValue = info['currency'].toString();
|
||
menuItems[1].selectedValue =
|
||
info['locale'] == null ? StringsLocalization.localeCode : info['locale'];
|
||
menuItems[2].selectedValue =
|
||
info['token'] == null ? '' : getTokenSuffix(info['token']);
|
||
});
|
||
});
|
||
return getMainWidget();
|
||
}
|
||
|
||
String getTokenSuffix(String token) {
|
||
return token.substring(token.length - 4, token.length);
|
||
}
|
||
|
||
Widget getMainWidget() {
|
||
return new Scaffold(appBar: getAppBar(),
|
||
body: getScreenContent());
|
||
}
|
||
|
||
@override
|
||
Widget getScreenContent() {
|
||
return new Container(
|
||
margin: new EdgeInsets.only(top: 16.0),
|
||
child: new ListView(children: getSettings()));
|
||
}
|
||
|
||
@override
|
||
List<Widget> getMenuButtons() {
|
||
return null;
|
||
}
|
||
|
||
List<Widget> getSettings() {
|
||
List<Widget> widgets = new List();
|
||
for (int i = 0; i < menuItems.length; i++) {
|
||
if (menuItems[i].selectedValue.toString() != '') {
|
||
widgets.add(
|
||
getSettingsItem(() => onPressed(menuItems.indexOf(menuItems[i])),
|
||
menuItems[i].title,
|
||
getValue(i)));
|
||
}
|
||
}
|
||
return widgets;
|
||
}
|
||
|
||
String getValue(int position) {
|
||
switch (position) {
|
||
case 0 :
|
||
return getCurrencyTitle(int.parse(menuItems[position].selectedValue));
|
||
case 1 :
|
||
return getLocaleTitle(menuItems[position].selectedValue);
|
||
default :
|
||
return null;
|
||
}
|
||
}
|
||
|
||
Widget getSettingsItem(VoidCallback onPressed, String title, String value) {
|
||
return new Container(
|
||
height: 56.0,
|
||
padding: new EdgeInsets.only(left: 8.0),
|
||
child: (new FlatButton(
|
||
onPressed: onPressed,
|
||
child: getRow(title, value))));
|
||
}
|
||
|
||
Widget getRow(String title, String value) {
|
||
if (value == null) {
|
||
return new Row(children: <Widget>[
|
||
new Text(title, style: new TextStyle(
|
||
fontWeight: FontWeight.w600,
|
||
color: faqGrey,
|
||
fontSize: 14.0))
|
||
]);
|
||
} else {
|
||
return new Row(children: <Widget>[
|
||
new Expanded(child: new Text(title, style: new TextStyle(
|
||
fontWeight: FontWeight.w600,
|
||
color: faqGrey,
|
||
fontSize: 14.0))),
|
||
new Text(value,
|
||
style: new TextStyle(
|
||
fontWeight: FontWeight.w400,
|
||
color: faqGrey,
|
||
fontSize: 14.0)),
|
||
getArrow()
|
||
]);
|
||
}
|
||
}
|
||
|
||
void onPressed(int position) {
|
||
switch (position) {
|
||
case 0 :
|
||
return pushRoute(context, new CurrenciesScreen(helper, app));
|
||
case 1 :
|
||
return pushRoute(context, new LanguagesScreen(helper, app));
|
||
case 2 :
|
||
return logout(context, helper);
|
||
}
|
||
}
|
||
|
||
// В методе отправляется запрос на удаление токена кассы, очищаются SharedPreferences приложения.
|
||
logout(BuildContext context, SqliteHelper helper) {
|
||
showYesNoDialog(context, StringsLocalization.confirmation(),
|
||
StringsLocalization.askChangeStore());
|
||
}
|
||
|
||
// Запуск диалога с двумя кнопками
|
||
showYesNoDialog(BuildContext context, String title, String content) {
|
||
showDialog<bool>(
|
||
context: context,
|
||
child: new AlertDialog(
|
||
title: new Text(title),
|
||
content: new Text(content),
|
||
actions: <Widget>[
|
||
new FlatButton(
|
||
child: new Text(StringsLocalization.no()),
|
||
onPressed: () {
|
||
Navigator.of(context).pop(false);
|
||
}),
|
||
new FlatButton(
|
||
child: new Text(StringsLocalization.yes()),
|
||
onPressed: () {
|
||
Navigator.of(context).pop(true);
|
||
})
|
||
])).then((b) {
|
||
if (b) {
|
||
helper.getToken().then((token) {
|
||
getDeleteTokenRequest(token).then((response) {
|
||
helper.clear().then((result) {
|
||
Navigator.of(context).pop('');
|
||
});
|
||
}).catchError((error) {
|
||
print(error.toString());
|
||
Navigator.of(context).pop('');
|
||
});
|
||
});
|
||
}
|
||
});
|
||
}
|
||
|
||
Widget getArrow() {
|
||
return new Container(margin: new EdgeInsets.only(left: 8.0),
|
||
child: new Image.asset(settings_arrow_png, height: 42.0));
|
||
}
|
||
|
||
@override
|
||
String getTitle() {
|
||
return StringsLocalization.settings() + " ($versionName)";
|
||
}
|
||
}
|