import 'package:flutter/material.dart'; import 'dart:async'; import 'dart:convert'; import 'common.dart'; import 'network.dart'; import 'consts.dart'; import 'registration.dart'; import 'activate_token.dart'; class SplashScreen extends StatelessWidget { @override Widget build(BuildContext context) { // Появляется splash screen, проверяется токен. new Future.delayed(const Duration(milliseconds: 500), () { showNextScreen(context); }); return new Stack(children: [getBackgroundContainer(), new Align(alignment: FractionalOffset.bottomRight, child: new Container(margin: new EdgeInsets.only(right: 11.0, bottom: 5.0), child: new Image.asset(powered_by_dinect_splash_png, height: 16.0, width: 122.0)))]); } getBackgroundContainer() { const margin = 48.0; return new Container(padding: new EdgeInsets.only(left: margin, right: margin), decoration: getSplashBackground()); } getSplashBackground() { return new BoxDecoration(image: new DecorationImage(image: new ExactAssetImage(splash_png), fit: BoxFit.cover)); } /// Запуск следующего экрана приложения. showNextScreen(BuildContext context) async { token = await platform.invokeMethod('getToken'); print('token: $token'); // В случае, если в приложении отсутствует токен, // необходимо запустить регистрацию кассы. if (token == null) { pushRoute(context, new RegistrationScreen()); } else { checkTokenStatus(token).then((statusResponse) { handleStatusResponse(context, statusResponse); }).catchError((error) { print(error.toString()); return false; }); } } // Обработка ответа. // В случае, если токен был удален может прийти active: false, либо 404. // Если токен не активен, попробовать создать его еще раз. В случае успешного создания токена удалить его и перейти на экран регистрации handleStatusResponse(BuildContext context, var statusResponse) async { int code = statusResponse.statusCode; print('resp: ${code}'); if (code == 404) { platform.invokeMethod('removeKeys').then((result) { print('try to start registration'); pushRoute(context, new RegistrationScreen()); }); } else { Map statusResponseMap = JSON.decode(statusResponse.body); bool active = statusResponseMap['active']; if (active) { startScanner(context); } else { createToken(await platform.invokeMethod('getMerchantID'), await platform.invokeMethod('getPosID')).then((response) { print('response.body: ${response.body}'); if (response.statusCode == 409) { pushRoute(context, new FinishRegistrationScreen()); } else if (response.statusCode == 201) { platform.invokeMethod('removeKeys').then((result) { Map parsedMap = JSON.decode(result); String t = parsedMap['token']; deleteToken(t).then((response) { print(response.body); Navigator.of(context).pop(); // Убираем текущий route pushRoute(context, new RegistrationScreen()); // Запускаем регистрацию }).catchError((error) { print(error.toString()); }); }); } }).catchError((error) => print(error.toString())); } } } }