aman-kassa-flutter/lib/views/login/login_view.dart

138 lines
5.7 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'dart:ui';
import 'package:aman_kassa_flutter/core/locator.dart';
import 'package:aman_kassa_flutter/core/services/dialog_service.dart';
import 'package:aman_kassa_flutter/redux/actions/user_actions.dart';
import 'package:aman_kassa_flutter/redux/state/user_state.dart';
import 'package:aman_kassa_flutter/redux/store.dart';
import 'package:aman_kassa_flutter/shared/app_colors.dart';
import 'package:aman_kassa_flutter/shared/ui_helpers.dart';
import 'package:aman_kassa_flutter/widgets/fields/busy_button.dart';
import 'package:aman_kassa_flutter/widgets/fields/input_field.dart';
import 'package:barcode_scan/gen/protos/protos.pb.dart';
import 'package:barcode_scan/gen/protos/protos.pbenum.dart';
import 'package:barcode_scan/model/scan_options.dart';
import 'package:barcode_scan/platform_wrapper.dart';
import 'package:flutter/services.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter/material.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
class LoginView extends StatelessWidget {
final emailController = TextEditingController(text: 'test@kkm-kassa.kz');
final passwordController = TextEditingController(text: 'qwe123');
final FocusNode passwordNode = new FocusNode();
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
final DialogService _dialogService = locator<DialogService>();
@override
Widget build(BuildContext context) {
return StoreConnector<AppState, UserState>(
converter: (store) => store.state.userState,
builder: (context, vm) {
return Scaffold(
key: _scaffoldKey,
backgroundColor: fillColor,
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 50),
child: Column(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Stack(
alignment: Alignment.bottomLeft,
children: <Widget>[
SizedBox(
height: 150,
child: Image.asset('assets/images/logo.png'),
),
Positioned(child: Text('онлайн касса', style: TextStyle(fontWeight: FontWeight.bold),), bottom: 23.0,left: 25.0,),
],
),
InputField(
placeholder: 'Электронная почта',
controller: emailController,
textInputType: TextInputType.emailAddress,
nextFocusNode: passwordNode,
additionalNote: vm.loginFormMessage.email,
),
verticalSpaceSmall,
InputField(
placeholder: 'Пароль',
password: true,
controller: passwordController,
fieldFocusNode: passwordNode,
additionalNote: vm.loginFormMessage.password,
enterPressed: _pressBtnEnter),
verticalSpaceMedium,
Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.end,
children: [
BusyButton(
title: 'Войти',
busy: vm.isLoading,
onPressed: _pressBtnEnter,
)
],
),
verticalSpaceLarge,
// TextLink(
// 'Регистрация',
// onPressed: () {},
// ),
IconButton(
icon: Icon(MdiIcons.qrcodeScan),
iconSize: 40,
tooltip: "Scan",
onPressed: scan,
)
],
),
));
});
}
_pressBtnEnter() async {
Redux.store
.dispatch(authenticate(emailController.text, passwordController.text));
}
Future<void> scan() async {
try {
var options = ScanOptions(strings: {
"cancel": 'Отмена',
"flash_on": 'Вкл фонарик',
"flash_off": 'Выкл фонарик',
});
var result = await BarcodeScanner.scan(options: options);
print(result.type); // The result type (barcode, cancelled, failed)
print(result.rawContent); // The barcode content
print(result.format); // The barcode format (as enum)
print(result
.formatNote); // If a unknown format was scanned this field contains a note
if (result.type == ResultType.Barcode &&
result.rawContent?.length == 60) {
Redux.store.dispatch(authenticateToken(result.rawContent));
} else {
_dialogService.showDialog(description: 'Не верный формат QR кода');
}
} on PlatformException catch (e) {
var result = ScanResult.create();
result.type = ResultType.Error;
result.format = BarcodeFormat.unknown;
if (e.code == BarcodeScanner.cameraAccessDenied) {
result.rawContent = 'The user did not grant the camera permission!';
_dialogService.showDialog(
description: 'Нет доступа до камеры устройства');
} else {
result.rawContent = 'Unknown error: $e';
_dialogService.showDialog(description: 'Неизвестная ошибка: $e');
}
}
}
}