import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'main.dart'; import 'activate_token.dart'; /// Экран регистрации магазина и кассы. class RegistrationScreen extends StatefulWidget { @override State createState() => new _RegistrationScreenState(); } class _RegistrationScreenState extends BaseState { String _merchantID = ""; @override Widget build(BuildContext context) { return new Scaffold(appBar: _getAppBar(), body: _getScreen(context)); } AppBar _getAppBar() { return new AppBar(title: new Text("Регистрация"), backgroundColor: primaryColor, actions: [ new IconButton( icon: new Icon(Icons.help_outline), tooltip: 'Air it', onPressed: faq, ) ]); } Widget _getScreen(BuildContext context) { return new Container(height: 332.0, child: new ListView(reverse: true, children: [ new Center(child: new Column(children: [ _getLogo(), _getDecoratedInputField(), _getButton(context)])) ].reversed.toList())); } Widget _getLogo() { return new Container(height: 192.0, width: 156.0, child: new Image.asset(logo_png, height: 24.0, width: 156.0)); } 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()); } 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)); } void _handleUserInput(String text) { if (text.length > 0) { setState(() { _merchantID = 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, _merchantID) : null, color: primaryColor)); } bool _isValidMerchantID() { return _merchantID.length == 5; } void _registerShop(BuildContext context, String merchantShop) { _register(context, merchantShop); } _register(BuildContext context, String merchantShop) 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'; var body = { 'merchant_shop': merchantShop, 'pos': pos, 'description': userAgent + '-' + pos }; print(url); for (var value in body.values) { print(value); } 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()); }); pushRoute(context, new FinishRegistrationScreen()); }).catchError((error) { print(error.toString()); }); } }