Регистрация, проверка токена, обработка ошибок, сканирование
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:convert'; // Пакет для обработки json с ответом от сервера.
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'main.dart';
|
||||
import 'dart:async';
|
||||
import 'activate_token.dart';
|
||||
|
||||
/// На фото мой сын, большой любитель голых констант.
|
||||
|
||||
/// Экран регистрации магазина и кассы.
|
||||
class RegistrationScreen extends StatefulWidget {
|
||||
@override State createState() => new _RegistrationScreenState();
|
||||
@@ -12,92 +14,125 @@ class RegistrationScreen extends StatefulWidget {
|
||||
|
||||
class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
|
||||
String _merchantID = "";
|
||||
String error = null;
|
||||
bool _loading = false;
|
||||
|
||||
@override Widget build(BuildContext context) {
|
||||
@override build(BuildContext context) {
|
||||
return new Scaffold(appBar: _getAppBar(), body: _getScreen(context));
|
||||
}
|
||||
|
||||
AppBar _getAppBar() {
|
||||
_getAppBar() {
|
||||
return new AppBar(title: new Text("Регистрация"),
|
||||
actions: <Widget>[new IconButton(icon: new Icon(Icons.help_outline), onPressed: () {})]);
|
||||
}
|
||||
|
||||
Widget _getScreen(BuildContext context) {
|
||||
_getScreen(BuildContext context) {
|
||||
return new Stack(children: <Widget>[_getScreenContent(), _getProgressIndicator()]);
|
||||
}
|
||||
|
||||
Widget _getScreenContent() {
|
||||
/// Высота контейнера задана для того, чтобы элементы располагались вверху экрана
|
||||
/// и список скроллился снизу вверх при открытии клавиатуры.
|
||||
_getScreenContent() {
|
||||
return new Container(height: 332.0,
|
||||
child: new ListView(reverse: true, children: <Widget>[
|
||||
new Center(child: new Column(children: <Widget>[
|
||||
_getLogo(),
|
||||
_getDecoratedInputField(),
|
||||
_getButton(context)]))
|
||||
].reversed.toList()));
|
||||
child: new ListView(reverse: true, children: <Widget>[
|
||||
new Center(child: new Column(children: <Widget>[
|
||||
_getLogo(),
|
||||
_getMerchantIDTitle(),
|
||||
_getDecoratedInputField(),
|
||||
_getButton(context)]))
|
||||
].reversed.toList()));
|
||||
}
|
||||
|
||||
Widget _getProgressIndicator() {
|
||||
/// Индикация отправки токена кассы.
|
||||
_getProgressIndicator() {
|
||||
return new Center(child: _loading ? new CircularProgressIndicator() : null);
|
||||
}
|
||||
|
||||
Widget _getLogo() {
|
||||
return new Container(height: 192.0, width: 156.0,
|
||||
child: new Image.asset(logo_png, height: 24.0, width: 156.0));
|
||||
/// Метод возвращает контейнер с отступами, который содержит картинку с логотипом.
|
||||
/// Картинка должна
|
||||
_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));
|
||||
}
|
||||
|
||||
Widget _getDecoratedInputField() {
|
||||
return new Container(margin: new EdgeInsets.only(left: 28.0, right: 28.0),
|
||||
padding: new EdgeInsets.only(top: 12.0, bottom: 12.0, left: 16.0, right: 16.0),
|
||||
decoration: _getDecoraionForInputField(),
|
||||
child: _getInputField());
|
||||
_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)))]));
|
||||
}
|
||||
|
||||
Widget _getInputField() {
|
||||
return new TextField(decoration: new InputDecoration.collapsed(hintText: merchantIDHint,
|
||||
hintStyle: new TextStyle(color: const Color(0xffa5a5a5), fontSize: 16.0)),
|
||||
onChanged: (text) => _handleUserInput(text));
|
||||
}
|
||||
|
||||
Decoration _getDecoraionForInputField() {
|
||||
return new BoxDecoration(color: Colors.white,
|
||||
border: new Border.all(color: const Color(0xffcfd8dc), width: 1.0,),
|
||||
borderRadius: new BorderRadius.all(new Radius.circular(4.0)));
|
||||
}
|
||||
|
||||
Widget _getButton(BuildContext context) {
|
||||
return new Container(margin: new EdgeInsets.only(top: 36.0), height: 42.0,
|
||||
padding: new EdgeInsets.only(left: 40.0, right: 40.0),
|
||||
child: new RaisedButton(child: new Text('ЗАРЕГИСТРИРОВАТЬ',
|
||||
style: new TextStyle(color: Colors.white)),
|
||||
onPressed: _isValidMerchantID() ? () => _registerShop(context) : null,
|
||||
color: primaryColor));
|
||||
}
|
||||
|
||||
Widget _getCircularProgressIndicator() {
|
||||
return new Center(child: new CircularProgressIndicator());
|
||||
}
|
||||
|
||||
_isValidMerchantID() {
|
||||
return merchantID.length == 5;
|
||||
}
|
||||
|
||||
_handleUserInput(String text) {
|
||||
if (text.length > 0) {
|
||||
setState(() {
|
||||
merchantID = text;
|
||||
});
|
||||
_getMerchantIDTitleText() {
|
||||
if (merchantID.length == 0 && error == null) {
|
||||
return ' ';
|
||||
} else if (error != null) {
|
||||
return error;
|
||||
} else {
|
||||
return 'ID Магазина';
|
||||
}
|
||||
}
|
||||
|
||||
_registerShop(BuildContext context) {
|
||||
/// Метод возвращает контейнер с установленными отступами, в котором размещен 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;
|
||||
double topMargin = 36.0;
|
||||
double horizontalPadding = 40.0; // Отступы по краям от кнопки.
|
||||
return new Container(margin: new EdgeInsets.only(top: topMargin), height: buttonHeight,
|
||||
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,
|
||||
color: primaryColor));
|
||||
}
|
||||
|
||||
/// Токен кассы - это DIN код. DIN код - это специальный код динекта, максимальная его длина - 25 символов.
|
||||
_isValidMerchantID() {
|
||||
return merchantID.length > 0 && merchantID.length < 25;
|
||||
}
|
||||
|
||||
/// Смена состояния экрана при изменении текста в поле ввода.
|
||||
_handleUserInput(String text) {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_registerDemo(context);
|
||||
merchantID = text;
|
||||
});
|
||||
}
|
||||
|
||||
/// Показать индикатор, запросить токен.
|
||||
_registerShop(BuildContext context) {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_register(context);
|
||||
});
|
||||
}
|
||||
|
||||
/// Экран зависает на 1 сек, после этого выполняется переход на экран потверждения токена.
|
||||
_registerDemo(BuildContext context) {
|
||||
new Future.delayed(const Duration(milliseconds: 1000), () {
|
||||
_loading = false;
|
||||
@@ -105,38 +140,44 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
});
|
||||
}
|
||||
|
||||
/// Получение от платформы id установки, формирование запроса на получение токена, сохранение токена.
|
||||
_register(BuildContext context) async {
|
||||
// const platform = const MethodChannel('com.dinect.checker/instance_id');
|
||||
// String url = intUrl + 'tokens/?_dmapptoken=' + intToken;
|
||||
// String pos = await platform.invokeMethod('getInstanceID');
|
||||
// print(pos);
|
||||
// String userAgent = 'dm-checker-test v1.0.1';
|
||||
const platform = const MethodChannel('com.dinect.checker/instance_id');
|
||||
|
||||
// var body = {
|
||||
// 'merchant_shop': merchantShop,
|
||||
// 'pos': pos,
|
||||
// 'description': userAgent + '-' + pos
|
||||
// };
|
||||
String url = intUrl + 'tokens/?_dmapptoken=' + intToken;
|
||||
String pos = (new DateTime.now().millisecondsSinceEpoch / 1000).toString();
|
||||
|
||||
// print(url);
|
||||
// Поле description - необязательное.
|
||||
var body = {
|
||||
'merchant_shop': merchantID,
|
||||
'pos': pos,
|
||||
};
|
||||
|
||||
// for (var value in body.values) {
|
||||
// print(value);
|
||||
// }
|
||||
httpClient.post(url, body: body).then((response) {
|
||||
|
||||
// httpClient.post(url, body: body).then((response) {
|
||||
// print(response.body);
|
||||
// Map parsedMap = JSON.decode(response.body);
|
||||
// token = parsedMap['token'];
|
||||
// platform.invokeMethod('saveToken', {'token' : token}).then((value) {
|
||||
// print(value.toString());
|
||||
// });
|
||||
// setState(() {
|
||||
// loading = false;
|
||||
// });
|
||||
// pushRoute(context, new FinishRegistrationScreen());
|
||||
// }).catchError((error) {
|
||||
// print(error.toString());
|
||||
// });
|
||||
setState(() {
|
||||
error = null;
|
||||
});
|
||||
|
||||
print(response.body);
|
||||
Map parsedMap = JSON.decode(response.body);
|
||||
setState(() {
|
||||
_loading = false;
|
||||
});
|
||||
if (response.statusCode == 201) {
|
||||
token = parsedMap['token'];
|
||||
platform.invokeMethod('saveToken', {'token' : token});
|
||||
platform.invokeMethod('saveMerchantID', {'merchantID' : merchantID});
|
||||
pushRoute(context, new FinishRegistrationScreen());
|
||||
} else {
|
||||
setState(() {
|
||||
error = parsedMap['errors'][0];
|
||||
});
|
||||
}
|
||||
}).catchError((error) {
|
||||
setState(() {
|
||||
error = 'Отсутствует интернет соединение';
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user