Files
checker/lib/screens/finish_registration.dart
2019-02-11 18:21:52 +08:00

132 lines
4.3 KiB
Dart
Raw Permalink 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 'dart:convert';
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/strings.dart';
import 'package:flutter/material.dart';
class FinishRegistrationScreen extends BaseScreen {
FinishRegistrationScreen(helper, app) : super(helper, app);
@override State createState() => new RegistrationScreenState(helper, app);
}
class RegistrationScreenState extends BaseState<FinishRegistrationScreen> {
RegistrationScreenState(SqliteHelper helper, String app) : super(helper, app);
bool _tokenActive = false;
String _merchantID = '';
bool isAutomaticallyImplyLeading() => false;
@override Widget build(BuildContext context) {
if (_merchantID == '') {
helper.getMerchantID().then((result) {
setState(() {
_merchantID = result;
});
});
}
return new WillPopScope(onWillPop: () => onWillPop(), child: getMainWidget());
}
onWillPop() {
if (Theme.of(context).platform != TargetPlatform.iOS) {
platform.invokeMethod('finish');
}
}
@override String getTitle() {
return StringsLocalization.registration();
}
@override getHintString() {
return _merchantID;
}
@override Widget getScreenContent() {
return new Column(children: <Widget>[
getLogo(),
getHintLabel(),
getInputField(),
getMessage(),
buildRaisedButton(_tokenActive
? StringsLocalization.completeRegistration()
: StringsLocalization.refreshActivationStatus(), () => handleTap())
]);
}
// Если токен активирован, то открывается экран со сканером,
// Если нет, то отправляется запрос на проверку статуса токена.
handleTap() {
print('tokenActive: $_tokenActive');
if (_tokenActive) {
helper.getToken().then((token) {
Navigator.of(context).pop(token);
});
} else {
platform.invokeMethod('isOnline').then((isOnline) {
if (isOnline ) {
helper.getToken().then((token) {
getCheckTokenStatusRequest(token).then((response) {
setState(() {
_tokenActive = json.decode(response.body)['active'];
});
}).catchError((error) {
print(error.toString());
return false;
});
});
}
});
}
}
@override getTextWidget() {
return new Row(
children: <Widget>[new Text(_merchantID != null ? _merchantID : '',
style: new TextStyle(color: Colors.black, fontSize: 16.0))
]);
}
/// Метод возвращает контейнер с текстом сообщения и бэкграундом.
getMessage() {
return new Container(height: _tokenActive ? 72.0 : 108.0,
decoration: _getDecorationForMessageField(),
margin: new EdgeInsets.only(top: 20.0, left: 12.0, right: 12.0),
padding: new EdgeInsets.only(bottom: 22.0, left: 14.0, right: 14.0),
child: new Center(child: getMessageTextWidget()));
}
/// Метод возвращает виджет с текстом сообщения, всеми его привязками и стилями.
getMessageTextWidget() {
return new Text(getMessageString(), textAlign: TextAlign.center,
style: new TextStyle(
height: 1.5, fontWeight: FontWeight.bold, fontSize: 14.0,
color: _tokenActive
? tokenActiveTextColor
: tokenActivateTextColor));
}
/// Получаем текст сообщения, в зависимости от статуса активации.
getMessageString() {
return _tokenActive
? StringsLocalization.completeRegistration()
: StringsLocalization.refreshActivationStatus();
}
/// Фоновое изображение для сообщения.
Decoration _getDecorationForMessageField() {
return new BoxDecoration(image: new DecorationImage(
image: new ExactAssetImage(
_tokenActive ? active_token_bg_png : activate_token_bg_png),
fit: _tokenActive ? BoxFit.fitWidth : BoxFit.fill));
}
}