Created
June 14, 2021 08:16
-
-
Save thanhhaiqtvn/92144c79388db5b6d6d6bee5da0fe6f3 to your computer and use it in GitHub Desktop.
Share code flutter synchronization screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'package:bar_rest_flutter/environments/environment.dart'; | |
import 'package:bar_rest_flutter/managers/ping_manager.dart'; | |
import 'package:bar_rest_flutter/managers/session_manager.dart'; | |
import 'package:bar_rest_flutter/managers/synchronize_manager.dart'; | |
import 'package:bar_rest_flutter/packages/dafluta/dafluta.dart'; | |
import 'package:bar_rest_flutter/resources/localizations.dart'; | |
import 'package:bar_rest_flutter/resources/palette.dart'; | |
import 'package:bar_rest_flutter/routes/routes.dart'; | |
import 'package:bar_rest_flutter/screens/home/home_screen.dart'; | |
import 'package:bar_rest_flutter/screens/login/login_screen.dart'; | |
import 'package:bar_rest_flutter/services/validator.dart'; | |
import 'package:bar_rest_flutter/storage/working_site_storage.dart'; | |
import 'package:bar_rest_flutter/widgets/custom_app_bar.dart'; | |
import 'package:bar_rest_flutter/widgets/custom_button.dart'; | |
import 'package:bar_rest_flutter/widgets/custom_input.dart'; | |
import 'package:bar_rest_flutter/widgets/custom_text_field.dart'; | |
import 'package:bar_rest_flutter/widgets/dialog/toast.dart'; | |
import 'package:bar_rest_flutter/widgets/error_message.dart'; | |
import 'package:bar_rest_flutter/widgets/labels/label.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart'; | |
class SynchronizationScreen extends StatelessWidget { | |
final SynchronizationState state = SynchronizationState(); | |
static PageRoute<SynchronizationScreen> instance() => | |
FadeRoute<SynchronizationScreen>(SynchronizationScreen()); | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: CustomAppBar( | |
Localized.get.synchronizationTitle, | |
), | |
body: new GestureDetector( | |
onTap: () { | |
FocusScope.of(context).requestFocus(new FocusNode()); | |
}, | |
child: StateProvider<SynchronizationState>( | |
state: state, | |
builder: (context, state) => LayoutBuilder( | |
builder: | |
(BuildContext context, BoxConstraints viewportConstraints) { | |
return Padding( | |
padding: EdgeInsets.only(left: 15, right: 15), | |
child: SingleChildScrollView( | |
child: ConstrainedBox( | |
constraints: BoxConstraints( | |
minHeight: viewportConstraints.maxHeight), | |
child: SafeArea( | |
child: Padding( | |
padding: EdgeInsets.only(top: 15, bottom: 15), | |
child: Column( | |
mainAxisSize: MainAxisSize.max, | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: <Widget>[ | |
ContentBody(state), | |
VBox(15), | |
ContentBottom(state), | |
], | |
), | |
), | |
), | |
), | |
), | |
); | |
}, | |
), | |
), | |
), | |
); | |
} | |
} | |
class ContentBody extends StatelessWidget { | |
final SynchronizationState state; | |
const ContentBody(this.state); | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: [ | |
Label( | |
text: Localized.get.synchronizationEnterUserTitle, | |
size: 20, | |
weight: FontWeight.bold, | |
), | |
VBox(15), | |
state.isShowingBarcode ? ContentBarCode(state) : ContentAccount(state), | |
VBox(15), | |
CustomButton( | |
title: Localized.get.buttonSignIn, | |
onPressed: () { | |
if (state.isShowingBarcode) { | |
state.loginWithBarcode(context); | |
} else { | |
state.loginWithCredentials(context); | |
} | |
}, | |
minWidth: 120, | |
), | |
VBox(15), | |
CustomButton( | |
title: state.isShowingBarcode | |
? Localized.get.synchronizationButtonLoginByUserAndPass | |
: Localized.get.synchronizationButtonLoginByBarCode, | |
onPressed: () => state.changeSignInMethod(), | |
minWidth: 120, | |
), | |
], | |
); | |
} | |
} | |
class ContentBottom extends StatelessWidget { | |
final SynchronizationState state; | |
const ContentBottom(this.state); | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: [ | |
CustomButton( | |
title: Localized.get.buttonExitApp, | |
backgroundColor: Palette.secondaryButton, | |
onPressed: () { | |
if (Platform.isIOS) { | |
exit(0); | |
} else { | |
SystemNavigator.pop(); | |
} | |
}, | |
minWidth: 120, | |
), | |
VBox(10), | |
CustomButton( | |
title: Localized.get.synchronizationButtonChangeWindowAccount, | |
onPressed: () { | |
Routes.pushAlone(LoginScreen.instance()); | |
}, | |
minWidth: 120, | |
), | |
], | |
); | |
} | |
} | |
class ContentAccount extends StatefulWidget { | |
final SynchronizationState state; | |
const ContentAccount(this.state); | |
@override | |
_ContentAccountState createState() => _ContentAccountState(); | |
} | |
class _ContentAccountState extends State<ContentAccount> { | |
final FocusNode focus = FocusNode(); | |
@override | |
void dispose() { | |
focus.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
child: Form( | |
key: widget.state.formKey, | |
child: Column( | |
children: [ | |
if (widget.state.hasError) ErrorMessage(widget.state.error), | |
CustomTextField( | |
hint: Localized.get.loginUsernamePlaceHolder, | |
type: TextInputType.text, | |
color: Palette.primaryText, | |
nextFocus: focus, | |
validator: _validateUsername, | |
controller: widget.state.userTextController, | |
), | |
VBox(15), | |
CustomTextField( | |
hint: Localized.get.loginPasswordPlaceHolder, | |
type: TextInputType.text, | |
isPassword: true, | |
color: Palette.primaryText, | |
focus: focus, | |
validator: _validatePassword, | |
controller: widget.state.passTextController, | |
), | |
], | |
), | |
), | |
); | |
} | |
String? _validateUsername(String? input) => Validator.userName(input ?? '') | |
? null | |
: Localized.get.validationErrorInvalidUsername; | |
String? _validatePassword(String? input) => Validator.password(input ?? '') | |
? null | |
: Localized.get.validationErrorInvalidPassword; | |
} | |
class ContentBarCode extends StatefulWidget { | |
final SynchronizationState state; | |
const ContentBarCode(this.state); | |
@override | |
_ContentBarCodeState createState() => _ContentBarCodeState(); | |
} | |
class _ContentBarCodeState extends State<ContentBarCode> { | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
margin: const EdgeInsets.fromLTRB(15, 0, 15, 0), | |
child: Row( | |
children: <Widget>[ | |
new Flexible( | |
child: new CustomInput( | |
hint: Localized.get.synchronizationBarcodePlaceHolder, | |
controller: widget.state.barcodeTextController, | |
maxLines: 1, | |
enabled: true), | |
), | |
const HBox(10), | |
IconButton( | |
onPressed: _scanQRcode, | |
icon: const Icon( | |
Icons.photo_camera_outlined, | |
color: Palette.black, | |
size: 45, | |
), | |
), | |
], | |
), | |
); | |
} | |
Future<void> _scanQRcode() async { | |
String barcodeScanRes; | |
try { | |
barcodeScanRes = await FlutterBarcodeScanner.scanBarcode( | |
'#ff6666', Localized.get.buttonCancel, true, ScanMode.QR); | |
print(barcodeScanRes); | |
} on PlatformException { | |
barcodeScanRes = Localized.get.synchronizationScanQRcodeFail; | |
} | |
if (!mounted) return; | |
setState(() { | |
widget.state.barcodeTextController.text = barcodeScanRes; | |
}); | |
} | |
} | |
class SynchronizationState extends BaseState { | |
late bool _isShowingBarcode; | |
final _formKey = GlobalKey<FormState>(); | |
String _error = ''; | |
final _userTextController = TextEditingController(); | |
final _passTextController = TextEditingController(); | |
final _barcodeTextController = TextEditingController(); | |
bool get isShowingBarcode => _isShowingBarcode; | |
GlobalKey<FormState> get formKey => _formKey; | |
String get error => _error; | |
bool get hasError => _error.isNotEmpty; | |
bool validate() => _formKey.currentState!.validate(); | |
TextEditingController get userTextController => _userTextController; | |
TextEditingController get passTextController => _passTextController; | |
TextEditingController get barcodeTextController => _barcodeTextController; | |
SynchronizationState() { | |
load(); | |
} | |
void load() { | |
_isShowingBarcode = false; | |
_initMock(); | |
} | |
_initMock() { | |
if (Environment.get.environment != 'prod') { | |
_userTextController.text = 'hvoquan'; | |
_passTextController.text = 'Swisscom123456'; | |
} | |
} | |
void showError(String error) { | |
_error = error; | |
notify(); | |
} | |
void changeSignInMethod() { | |
_isShowingBarcode = !_isShowingBarcode; | |
notify(); | |
} | |
Future loginWithBarcode(BuildContext context) async { | |
} | |
Future loginWithCredentials(BuildContext context) async { | |
} | |
_startServicesAndGoToHomeScreen() { | |
PingManager.start(); | |
SynchronizeManager.start(); | |
Routes.pushAlone(HomeScreen.instance()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment