gecko/lib/screens/myWallets/unlocking_wallet.dart

283 lines
11 KiB
Dart
Raw Normal View History

// ignore_for_file: avoid_print
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:gecko/models/chest_data.dart';
import 'package:gecko/providers/substrate_sdk.dart';
import 'package:gecko/providers/my_wallets.dart';
2021-11-14 19:21:20 +01:00
import 'package:gecko/models/wallet_data.dart';
import 'package:gecko/providers/wallet_options.dart';
import 'package:flutter/material.dart';
2022-05-26 02:19:29 +02:00
import 'package:gecko/providers/wallets_profiles.dart';
2021-11-15 03:55:08 +01:00
import 'package:gecko/screens/myWallets/choose_chest.dart';
import 'package:gecko/screens/myWallets/choose_wallet.dart';
2021-12-24 15:27:38 +01:00
import 'package:gecko/screens/myWallets/wallets_home.dart';
2022-05-26 02:19:29 +02:00
import 'package:gecko/screens/transaction_in_progress.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(
2021-12-23 12:36:09 +01:00
{Key? keyUnlockWallet, required this.wallet, required this.action})
2021-04-03 00:07:03 +02:00
: super(key: keyUnlockWallet);
2021-12-23 12:36:09 +01:00
WalletData? wallet;
2022-05-19 13:44:22 +02:00
late int currentChestNumber;
late ChestData currentChest;
2021-04-03 00:07:03 +02:00
String action;
// ignore: close_sinks
2021-12-23 12:36:09 +01:00
StreamController<ErrorAnimationType>? errorController;
final formKey = GlobalKey<FormState>();
2021-12-23 12:36:09 +01:00
Color? pinColor = const Color(0xffF9F9F1);
var walletPin = '';
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
WalletOptionsProvider _walletOptions =
Provider.of<WalletOptionsProvider>(context);
// final double statusBarHeight = MediaQuery.of(context).padding.top;
2021-04-03 02:39:09 +02:00
2022-05-19 13:44:22 +02:00
currentChestNumber = configBox.get('currentChest');
currentChest = chestBox.get(currentChestNumber)!;
int _pinLenght = _walletOptions.getPinLenght(wallet!.number);
errorController = StreamController<ErrorAnimationType>();
return Scaffold(
// backgroundColor: Colors.brown[600],
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Stack(children: <Widget>[
Positioned(
top: 10, //statusBarHeight + 10,
left: 15,
child: Builder(
builder: (context) => IconButton(
key: const Key('popButton'),
icon: const Icon(
Icons.arrow_back,
color: Colors.black,
size: 30,
),
onPressed: () => Navigator.pop(context),
),
),
),
Column(children: <Widget>[
SizedBox(height: isTall ? 100 : 20),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
currentChest.imageFile == null
? Image.asset(
'assets/chests/${currentChest.imageName}',
width: isTall ? 130 : 100,
)
: Image.file(
2021-12-23 12:36:09 +01:00
currentChest.imageFile!,
width: isTall ? 130 : 100,
),
const SizedBox(width: 5),
SizedBox(
width: 250,
child: Text(
2021-12-23 12:36:09 +01:00
currentChest.name!,
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 25,
color: Colors.black,
fontWeight: FontWeight.w700),
)),
]),
SizedBox(height: 30 * ratio),
const SizedBox(
width: 400,
2021-12-03 10:25:36 +01:00
child: Text(
'Pour déverrouiller votre coffre, composez votre code secret à labri des lézards indiscrets :',
2021-12-03 10:25:36 +01:00
style: TextStyle(
fontSize: 19,
color: Colors.black,
fontWeight: FontWeight.w400),
)),
SizedBox(height: 40 * ratio),
2022-05-19 13:44:22 +02:00
pinForm(context, _pinLenght),
SizedBox(height: 3 * ratio),
InkWell(
key: const Key('chooseChest'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return ChooseChest(action: action);
}),
);
},
child: SizedBox(
width: 400,
height: 70,
child: Center(
child: Text(
'Changer de coffre',
style: TextStyle(
fontSize: 22,
color: orangeC,
fontWeight: FontWeight.w600),
),
),
)),
]),
]),
]),
));
}
2022-05-19 13:44:22 +02:00
Widget pinForm(context, _pinLenght) {
// 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);
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
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) {
2021-12-23 12:36:09 +01:00
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 {
_myWalletProvider.pinCode = _pin.toUpperCase();
final isValid = await _sub.checkPassword(
currentChest.address!, _pin.toUpperCase());
2021-04-03 00:07:03 +02:00
if (!isValid) {
await Future.delayed(const Duration(milliseconds: 50));
errorController.add(ErrorAnimationType
.shake); // Triggering error shake animation
pinColor = Colors.red[600];
_myWalletProvider.pinCode = _myWalletProvider.mnemonic = '';
_walletOptions.reloadBuild();
2021-11-17 06:20:23 +01:00
pinFocus.requestFocus();
} else {
pinColor = Colors.green[400];
2022-05-25 20:40:55 +02:00
switch (action) {
case "mywallets":
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return const WalletsHome();
}),
);
break;
case "changeWallet":
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return ChooseWalletScreen(
chest: currentChestNumber, pin: _pin.toUpperCase());
}),
);
break;
case "pay":
2022-05-26 02:19:29 +02:00
// Payment workflow !
WalletsProfilesProvider _walletViewProvider =
Provider.of<WalletsProfilesProvider>(context,
listen: false);
final acc = _sub.getCurrentWallet();
log.d(
"fromAddress: ${acc.address!},destAddress: ${_walletViewProvider.pubkey!}, amount: ${double.parse(_walletViewProvider.payAmount.text)}, password: $_pin");
_sub.pay(
2022-05-26 02:19:29 +02:00
fromAddress: acc.address!,
destAddress: _walletViewProvider.pubkey!,
2022-05-26 02:19:29 +02:00
amount:
double.parse(_walletViewProvider.payAmount.text),
password: _pin.toUpperCase());
2022-05-25 20:40:55 +02:00
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return const TransactionInProgress();
2022-05-25 20:40:55 +02:00
}),
);
break;
case "cert":
WalletsProfilesProvider _walletViewProvider =
Provider.of<WalletsProfilesProvider>(context,
listen: false);
final acc = _sub.getCurrentWallet();
_sub.certify(acc.address!, _pin.toUpperCase(),
_walletViewProvider.pubkey!);
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return const TransactionInProgress(transType: 'cert');
}),
);
2022-05-25 20:40:55 +02:00
break;
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);
}
},
)),
);
}
}