Files
checker/lib/registration.dart

78 lines
2.8 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'package:flutter/material.dart';
import 'dart:convert'; // Пакет для обработки json с ответом от сервера.
import 'common.dart';
import 'network.dart';
import 'consts.dart';
import 'activate_token.dart';
import 'base_state.dart';
/// Экран регистрации магазина и кассы.
class RegistrationScreen extends StatefulWidget {
@override State createState() => new _RegistrationScreenState();
}
class _RegistrationScreenState extends BaseState<RegistrationScreen> {
@override String getTitle() {
return "Регистрация";
}
@override getHint() {
return 'ID магазина';
}
/// Высота контейнера задана для того, чтобы элементы располагались вверху экрана
/// и список скроллился снизу вверх при открытии клавиатуры.
@override Widget getScreenContent() {
return new Container(
child: new ListView(children: <Widget>[
new Center(child: new Column(children: <Widget>[
getLogo(),
getHintLabel(),
getDecoratedTextWidget(),
new Container(margin: new EdgeInsets.only(top: 36.0), child: buildRaisedButton(context, 'ЗАРЕГИСТРИРОВАТЬ', _isValidMerchantID() && !loading ? () => _registerShop(context) : null))]))
]));
}
/// Токен кассы - это DIN код. DIN код - это специальный код динекта, максимальная его длина - 25 символов.
_isValidMerchantID() {
print("${textFieldValue.length}");
return textFieldValue.length > 0 && textFieldValue.length < 25;
}
/// Показать progressBar, запросить токен.
_registerShop(BuildContext context) {
setState(() {
loading = true;
_register(context);
});
}
/// Получение от платформы id установки, формирование запроса на получение токена, сохранение токена.
_register(BuildContext context) async {
createToken(textFieldValue, await platform.invokeMethod('getPosID')).then((response) {
setState(() {
error = null;
loading = false;
});
print(response.body);
Map parsedMap = JSON.decode(response.body);
if (response.statusCode == 201) {
token = parsedMap['token'];
platform.invokeMethod('saveToken', {'token' : token});
platform.invokeMethod('saveMerchantID', {'merchantID' : textFieldValue});
pushRoute(context, new FinishRegistrationScreen());
} else {
setState(() {
error = parsedMap['errors'][0];
});
}
}).catchError((error) {
print(error.toString());
});
}
}