gecko/lib/screens/myWallets/unlocking_wallet.dart

254 lines
9.0 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:dubp/dubp.dart';
import 'package:flutter/services.dart';
import 'package:gecko/models/chest_data.dart';
2021-11-30 01:25:48 +01:00
import 'package:gecko/models/wallets_profiles.dart';
2021-11-14 19:21:20 +01:00
import 'package:gecko/models/my_wallets.dart';
import 'package:gecko/models/wallet_data.dart';
import 'package:gecko/models/wallet_options.dart';
import 'package:flutter/material.dart';
2021-12-01 08:14:07 +01:00
import 'package:gecko/screens/myWallets/cesium_wallet_options.dart';
2021-11-15 03:55:08 +01:00
import 'package:gecko/screens/myWallets/choose_chest.dart';
import 'package:pin_code_fields/pin_code_fields.dart';
import 'package:provider/provider.dart';
2021-04-02 12:05:37 +02:00
import 'package:gecko/globals.dart';
// ignore: must_be_immutable
class UnlockingWallet extends StatelessWidget {
2021-04-03 00:07:03 +02:00
UnlockingWallet(
{Key keyUnlockWallet, @required this.wallet, @required this.action})
: super(key: keyUnlockWallet);
2021-03-31 02:38:44 +02:00
WalletData wallet;
2021-04-03 00:07:03 +02:00
String action;
// ignore: close_sinks
StreamController<ErrorAnimationType> errorController;
final formKey = GlobalKey<FormState>();
2021-11-14 19:21:20 +01:00
var pinColor = const Color(0xffF9F9F1);
var walletPin = '';
String resultPay;
Future<NewWallet> get badWallet => null;
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
WalletOptionsProvider _walletOptions =
Provider.of<WalletOptionsProvider>(context);
2021-04-03 02:39:09 +02:00
2021-11-15 03:55:08 +01:00
int _pinLenght;
ChestData currentChest = chestBox.get(configBox.get('currentChest'));
if (currentChest.isCesium) {
_pinLenght = _walletOptions.getPinLenght(currentChest.dewif);
wallet = WalletData(derivation: -1, chest: currentChest.key);
} else {
_pinLenght = _walletOptions.getPinLenght(wallet.number);
}
errorController = StreamController<ErrorAnimationType>();
return Scaffold(
// backgroundColor: Colors.brown[600],
body: SafeArea(
child: Column(children: <Widget>[
Expanded(
child: Column(children: <Widget>[
2021-11-21 02:52:01 +01:00
SizedBox(height: isTall ? 100 : 20),
Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
2021-11-17 06:20:23 +01:00
currentChest.imageFile == null
? Image.asset(
'assets/chests/${currentChest.imageName}',
width: isTall ? 130 : 100,
)
: Image.file(
currentChest.imageFile,
width: isTall ? 130 : 100,
),
const SizedBox(width: 5),
SizedBox(
width: 250,
child: Text(
currentChest.name,
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 25,
color: Colors.black,
fontWeight: FontWeight.w700),
)),
]),
2021-11-15 18:05:08 +01:00
SizedBox(height: 30 * ratio),
const SizedBox(
width: 400,
child: Text(
'Pour déverrouiller votre coffre, composez votre code secret à labri des lézards indiscrets :',
style: TextStyle(
fontSize: 19,
color: Colors.black,
fontWeight: FontWeight.w400),
)),
2021-11-15 18:05:08 +01:00
SizedBox(height: 40 * ratio),
2021-12-01 08:14:07 +01:00
pinForm(context, _pinLenght, currentChest),
2021-11-15 18:05:08 +01:00
SizedBox(height: 3 * ratio),
2021-11-15 03:55:08 +01:00
InkWell(
key: const Key('chooseChest'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
2021-12-03 10:25:36 +01:00
return ChooseChest(action: action);
2021-11-15 03:55:08 +01:00
}),
);
},
child: SizedBox(
width: 400,
height: 70,
child: Center(
2021-12-03 10:25:36 +01:00
child: Text(
'Changer de coffre',
style: TextStyle(
fontSize: 22,
color: orangeC,
fontWeight: FontWeight.w600),
),
),
2021-11-15 03:55:08 +01:00
)),
]),
),
]),
));
}
2021-12-01 08:14:07 +01:00
Widget pinForm(context, _pinLenght, ChestData currentChest) {
// var _walletPin = '';
// ignore: close_sinks
StreamController<ErrorAnimationType> errorController =
StreamController<ErrorAnimationType>();
TextEditingController _enterPin = TextEditingController();
WalletOptionsProvider _walletOptions =
Provider.of<WalletOptionsProvider>(context);
2021-04-03 00:07:03 +02:00
MyWalletsProvider _myWalletProvider =
Provider.of<MyWalletsProvider>(context);
2021-11-30 01:25:48 +01:00
WalletsProfilesProvider _historyProvider =
Provider.of<WalletsProfilesProvider>(context);
2021-11-17 06:20:23 +01:00
FocusNode pinFocus = FocusNode();
return Form(
key: formKey,
child: Padding(
2021-11-15 18:05:08 +01:00
padding: EdgeInsets.symmetric(vertical: 5 * ratio, horizontal: 30),
child: PinCodeTextField(
2021-11-17 06:20:23 +01:00
focusNode: pinFocus,
autoFocus: true,
appContext: context,
pastedTextStyle: TextStyle(
color: Colors.green.shade600,
fontWeight: FontWeight.bold,
),
length: _pinLenght,
obscureText: true,
obscuringCharacter: '*',
animationType: AnimationType.fade,
validator: (v) {
if (v.length < _pinLenght) {
return "Votre code PIN fait $_pinLenght caractères";
} else {
return null;
}
},
pinTheme: PinTheme(
activeColor: pinColor,
borderWidth: 4,
shape: PinCodeFieldShape.box,
borderRadius: BorderRadius.circular(5),
2021-11-15 18:05:08 +01:00
fieldHeight: 50 * ratio,
fieldWidth: 50,
2021-11-17 06:20:23 +01:00
activeFillColor: Colors.black,
),
cursorColor: Colors.black,
2021-11-14 19:21:20 +01:00
animationDuration: const Duration(milliseconds: 300),
textStyle: const TextStyle(fontSize: 20, height: 1.6),
backgroundColor: const Color(0xffF9F9F1),
enableActiveFill: false,
errorAnimationController: errorController,
controller: _enterPin,
keyboardType: TextInputType.text,
2021-11-14 19:21:20 +01:00
boxShadows: const [
BoxShadow(
offset: Offset(0, 1),
color: Colors.black12,
blurRadius: 10,
)
],
onCompleted: (_pin) async {
2021-04-02 12:05:37 +02:00
log.d("Completed");
2021-04-03 02:39:09 +02:00
_myWalletProvider.pinCode = _pin;
2021-11-15 03:55:08 +01:00
final String resultWallet = await _walletOptions.readLocalWallet(
2021-11-14 19:21:20 +01:00
context, wallet, _pin.toUpperCase(), _pinLenght);
2021-04-03 00:07:03 +02:00
// _myWalletProvider.pinCode = _pin.toUpperCase();
_myWalletProvider.pinLenght = _pinLenght;
if (resultWallet == 'bad') {
errorController.add(ErrorAnimationType
.shake); // Triggering error shake animation
pinColor = Colors.red[600];
_walletOptions.reloadBuild();
2021-11-17 06:20:23 +01:00
pinFocus.requestFocus();
} else {
pinColor = Colors.green[400];
// await Future.delayed(Duration(milliseconds: 50));
2021-04-03 00:07:03 +02:00
if (action == "mywallets") {
2021-12-01 08:14:07 +01:00
currentChest.isCesium
? Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return CesiumWalletOptions(
cesiumWallet: currentChest);
}),
)
: Navigator.pushNamed(
formKey.currentContext, '/mywallets');
2021-04-03 00:07:03 +02:00
} else if (action == "pay") {
resultPay =
await _historyProvider.pay(context, _pin.toUpperCase());
await _paymentsResult(context);
2021-04-03 00:07:03 +02:00
}
}
},
onChanged: (value) {
2021-11-14 19:21:20 +01:00
if (pinColor != const Color(0xFFA4B600)) {
pinColor = const Color(0xFFA4B600);
}
},
)),
);
}
Future<bool> _paymentsResult(context) {
2021-12-03 10:25:36 +01:00
if (resultPay != "Success") log.i(resultPay);
return showDialog<bool>(
context: context,
barrierDismissible: true, // user must tap button!
builder: (BuildContext context) {
return AlertDialog(
title: Text(resultPay == "Success"
? 'Paiement effecuté avec succès !'
: "Une erreur s'est produite lors du paiement"),
2021-11-14 19:21:20 +01:00
content: const SingleChildScrollView(child: Text('')),
actions: <Widget>[
TextButton(
2021-11-14 19:21:20 +01:00
child: const Text("OK"),
onPressed: () {
Navigator.popUntil(
context,
ModalRoute.withName('/'),
);
},
),
],
);
},
);
}
}