Вынес дублирующийся код в базовый класс
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import 'dart:convert'; // Пакет для обработки json с ответом от сервера.
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'main.dart';
|
||||
import 'dart:convert'; // Пакет для обработки json с ответом от сервера.
|
||||
import 'dart:async';
|
||||
|
||||
import 'main.dart';
|
||||
import 'activate_token.dart';
|
||||
import 'base_state.dart';
|
||||
|
||||
/// На фото мой сын, большой любитель голых констант.
|
||||
|
||||
@@ -12,22 +14,26 @@ class RegistrationScreen extends StatefulWidget {
|
||||
@override State createState() => new _RegistrationScreenState();
|
||||
}
|
||||
|
||||
class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
|
||||
String error = null;
|
||||
bool _loading = false;
|
||||
class _RegistrationScreenState extends BaseState<RegistrationScreen> {
|
||||
|
||||
@override build(BuildContext context) {
|
||||
return new Scaffold(appBar: _getAppBar(), body: _getScreen(context));
|
||||
return new Scaffold(appBar: getAppBar(), body: _getScreen(context));
|
||||
}
|
||||
|
||||
_getAppBar() {
|
||||
return new AppBar(title: new Text("Регистрация"),
|
||||
actions: <Widget>[new IconButton(icon: new Icon(Icons.help_outline), onPressed: () {})]);
|
||||
@override String getTitle() {
|
||||
return "Регистрация";
|
||||
}
|
||||
|
||||
@override getHint() {
|
||||
return 'ID merchant';
|
||||
}
|
||||
|
||||
@overide getMenuButtons() {
|
||||
return <Widget>[new IconButton(icon: new Icon(Icons.help_outline), onPressed: () {})];
|
||||
}
|
||||
|
||||
_getScreen(BuildContext context) {
|
||||
return new Stack(children: <Widget>[_getScreenContent(), _getProgressIndicator()]);
|
||||
return new Stack(children: <Widget>[_getScreenContent(), getProgressIndicator()]);
|
||||
}
|
||||
|
||||
/// Высота контейнера задана для того, чтобы элементы располагались вверху экрана
|
||||
@@ -36,69 +42,13 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
return new Container(height: 332.0,
|
||||
child: new ListView(reverse: true, children: <Widget>[
|
||||
new Center(child: new Column(children: <Widget>[
|
||||
_getLogo(),
|
||||
_getMerchantIDTitle(),
|
||||
_getDecoratedInputField(),
|
||||
getLogo(),
|
||||
getHintLabel(),
|
||||
getDecoratedTextWidget(),
|
||||
_getButton(context)]))
|
||||
].reversed.toList()));
|
||||
}
|
||||
|
||||
/// Индикация отправки токена кассы.
|
||||
_getProgressIndicator() {
|
||||
return new Center(child: _loading ? new CircularProgressIndicator() : null);
|
||||
}
|
||||
|
||||
/// Метод возвращает контейнер с отступами, который содержит картинку с логотипом.
|
||||
/// Картинка должна
|
||||
_getLogo() {
|
||||
double containerHeight = 162.0;
|
||||
double imageHeight = 24.0;
|
||||
double imageWidth = 156.0;
|
||||
return new Container(height: containerHeight, child: new Image.asset(logo_png, height: imageHeight, width: imageWidth));
|
||||
}
|
||||
|
||||
_getMerchantIDTitle() {
|
||||
return new Container(margin: new EdgeInsets.only(top: 8.0, bottom: 8.0, left: 28.0, right: 28.0),
|
||||
child: new Row(crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[new Container(padding: new EdgeInsets.only(right: 8.0), child: new Text(_getMerchantIDTitleText(), overflow: TextOverflow.ellipsis, textAlign: TextAlign.left,
|
||||
style: new TextStyle(fontWeight: FontWeight.w300, color: error == null ? greyTextColor : primaryColor, fontSize: 14.0)))]));
|
||||
}
|
||||
|
||||
_getMerchantIDTitleText() {
|
||||
if (merchantID.length == 0 && error == null) {
|
||||
return ' ';
|
||||
} else if (error != null) {
|
||||
return error;
|
||||
} else {
|
||||
return 'ID Магазина';
|
||||
}
|
||||
}
|
||||
|
||||
/// Метод возвращает контейнер с установленными отступами, в котором размещен TextField обернутый в BoxDecoration.
|
||||
_getDecoratedInputField() {
|
||||
double margin = 28.0;
|
||||
double verticalPadding = 12.0;
|
||||
double horizontalPadding = 16.0;
|
||||
return new Container(margin: new EdgeInsets.only(left: margin, right: margin),
|
||||
padding: new EdgeInsets.only(top: verticalPadding, bottom: verticalPadding, left: horizontalPadding, right: horizontalPadding),
|
||||
decoration: _getDecoraionForInputField(),
|
||||
child: _getInputField());
|
||||
}
|
||||
|
||||
/// Метод возвращает TextField для _getDecoratedInputField
|
||||
_getInputField() {
|
||||
return new TextField(keyboardType: TextInputType.number, decoration: new InputDecoration.collapsed(hintText: merchantIDHint,
|
||||
hintStyle: new TextStyle(color: greyTextColor, fontSize: 16.0)),
|
||||
onChanged: (text) => _handleUserInput(text));
|
||||
}
|
||||
|
||||
/// Метод возвращает BoxDecoration для _getDecoratedInputField
|
||||
_getDecoraionForInputField() {
|
||||
return new BoxDecoration(color: textFieldBackground,
|
||||
border: new Border.all(color: textBorderColor, width: 1.0,),
|
||||
borderRadius: new BorderRadius.all(new Radius.circular(4.0)));
|
||||
}
|
||||
|
||||
/// Метод возвращает кнопку, которая запускает отправку токена кассы на сервер.
|
||||
_getButton(BuildContext context) {
|
||||
double buttonHeight = 42.0;
|
||||
@@ -108,26 +58,26 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
padding: new EdgeInsets.only(left: horizontalPadding, right: horizontalPadding),
|
||||
child: new RaisedButton(child: new Text('ЗАРЕГИСТРИРОВАТЬ',
|
||||
style: new TextStyle(color: Colors.white)),
|
||||
onPressed: _isValidMerchantID() && !_loading ? () => _registerShop(context) : null,
|
||||
onPressed: _isValidMerchantID() && !loading ? () => _registerShop(context) : null,
|
||||
color: primaryColor));
|
||||
}
|
||||
|
||||
/// Токен кассы - это DIN код. DIN код - это специальный код динекта, максимальная его длина - 25 символов.
|
||||
_isValidMerchantID() {
|
||||
return merchantID.length > 0 && merchantID.length < 25;
|
||||
return textFieldValue.length > 0 && textFieldValue.length < 25;
|
||||
}
|
||||
|
||||
/// Смена состояния экрана при изменении текста в поле ввода.
|
||||
_handleUserInput(String text) {
|
||||
setState(() {
|
||||
merchantID = text;
|
||||
textFieldValue = text;
|
||||
});
|
||||
}
|
||||
|
||||
/// Показать индикатор, запросить токен.
|
||||
_registerShop(BuildContext context) {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
loading = true;
|
||||
_register(context);
|
||||
});
|
||||
}
|
||||
@@ -135,7 +85,7 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
/// Экран зависает на 1 сек, после этого выполняется переход на экран потверждения токена.
|
||||
_registerDemo(BuildContext context) {
|
||||
new Future.delayed(const Duration(milliseconds: 1000), () {
|
||||
_loading = false;
|
||||
loading = false;
|
||||
pushRoute(context, new FinishRegistrationScreen());
|
||||
});
|
||||
}
|
||||
@@ -149,7 +99,7 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
|
||||
// Поле description - необязательное.
|
||||
var body = {
|
||||
'merchant_shop': merchantID,
|
||||
'merchant_shop': textFieldValue,
|
||||
'pos': pos,
|
||||
};
|
||||
|
||||
@@ -162,7 +112,7 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
print(response.body);
|
||||
Map parsedMap = JSON.decode(response.body);
|
||||
setState(() {
|
||||
_loading = false;
|
||||
loading = false;
|
||||
});
|
||||
if (response.statusCode == 201) {
|
||||
token = parsedMap['token'];
|
||||
|
||||
Reference in New Issue
Block a user