gecko/lib/screens/myWallets/wallets_home.dart

462 lines
16 KiB
Dart
Raw Normal View History

2021-02-27 20:29:35 +01:00
import 'package:flutter/services.dart';
import 'package:gecko/globals.dart';
2021-11-14 19:21:20 +01:00
import 'package:gecko/models/chest_data.dart';
import 'package:gecko/models/my_wallets.dart';
2021-11-11 22:38:28 +01:00
import 'package:gecko/models/queries.dart';
2021-11-14 19:21:20 +01:00
import 'package:gecko/models/wallet_data.dart';
import 'package:gecko/models/wallet_options.dart';
import 'package:flutter/material.dart';
2021-11-14 19:21:20 +01:00
import 'package:gecko/screens/common_elements.dart';
import 'package:gecko/screens/myWallets/choose_chest.dart';
import 'package:gecko/screens/myWallets/wallet_options.dart';
import 'package:gecko/screens/onBoarding/0_no_keychain_found.dart';
2021-11-11 22:38:28 +01:00
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:provider/provider.dart';
2021-01-26 21:00:26 +01:00
class WalletsHome extends StatelessWidget {
2021-02-15 01:44:25 +01:00
final _derivationKey = GlobalKey<FormState>();
2021-11-14 19:21:20 +01:00
WalletsHome({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
2021-02-27 20:29:35 +01:00
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
MyWalletsProvider myWalletProvider =
Provider.of<MyWalletsProvider>(context);
2021-11-14 04:33:59 +01:00
final int _currentChestNumber = myWalletProvider.getCurrentChest();
final ChestData _currentChest = chestBox.get(_currentChestNumber);
bool isWalletsExists;
2021-11-14 04:33:59 +01:00
if (!_currentChest.isCesium) {
myWalletProvider.listWallets =
myWalletProvider.readAllWallets(_currentChestNumber);
isWalletsExists = myWalletProvider.checkIfWalletExist();
}
2021-01-26 21:00:26 +01:00
2021-04-03 00:07:03 +02:00
return WillPopScope(
2021-11-09 04:22:04 +01:00
onWillPop: () {
Navigator.popUntil(
context,
ModalRoute.withName('/'),
);
return Future<bool>.value(true);
},
child: Scaffold(
appBar: AppBar(
leading: IconButton(
2021-11-14 19:21:20 +01:00
icon: const Icon(Icons.arrow_back, color: Colors.black),
2021-11-09 04:22:04 +01:00
onPressed: () {
Navigator.popUntil(
context,
ModalRoute.withName('/'),
);
}),
2021-11-14 04:33:59 +01:00
title: Text(_currentChest.name,
2021-11-14 19:21:20 +01:00
key: const Key('myWallets'),
style: TextStyle(color: Colors.grey[850])),
backgroundColor: const Color(0xffFFD58D),
2021-11-09 04:22:04 +01:00
),
body: SafeArea(
2021-11-14 04:33:59 +01:00
child: !isWalletsExists
2021-11-14 19:21:20 +01:00
? const NoKeyChainScreen()
2021-11-14 04:33:59 +01:00
: _currentChest.isCesium
? cesiumWalletOptions(context)
: myWalletsTiles(context),
2021-11-09 04:22:04 +01:00
),
),
);
}
2021-11-14 04:33:59 +01:00
Widget cesiumWalletOptions(BuildContext context) {
2021-11-14 19:21:20 +01:00
return Column(children: const [
2021-11-14 04:33:59 +01:00
Center(child: Text('This is a Cesium wallet')),
]);
}
2021-11-09 04:22:04 +01:00
Widget chestOptions(BuildContext context) {
return Column(children: [
2021-11-14 19:21:20 +01:00
const SizedBox(height: 50),
2021-11-09 04:22:04 +01:00
SizedBox(
height: 120,
width: 445,
child: ElevatedButton.icon(
icon: Image.asset(
'assets/chests/config.png',
),
style: ElevatedButton.styleFrom(
elevation: 2,
primary: floattingYellow, // background
onPrimary: Colors.black, // foreground
),
onPressed: () => null,
2021-11-14 19:21:20 +01:00
label: const Text(
" Paramétrer ce coffre",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.w700,
color: Color(0xff8a3c0f),
),
),
)),
2021-11-14 19:21:20 +01:00
const SizedBox(height: 30),
SizedBox(
height: 120,
width: 445,
child: ElevatedButton.icon(
icon: Image.asset('assets/chests/miniChests.png'),
2021-11-09 04:22:04 +01:00
style: ElevatedButton.styleFrom(
elevation: 2,
primary: floattingYellow, // background
onPrimary: Colors.black, // foreground
2021-04-03 00:07:03 +02:00
),
2021-11-09 04:22:04 +01:00
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return ChooseChest();
}),
),
2021-11-14 19:21:20 +01:00
label: const Text(
" Changer de coffre",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.w700,
color: Color(0xff8a3c0f),
),
2021-11-09 04:22:04 +01:00
),
)),
2021-11-14 19:21:20 +01:00
const SizedBox(height: 30)
2021-11-09 04:22:04 +01:00
]);
}
Widget myWalletsTiles(BuildContext context) {
MyWalletsProvider _myWalletProvider =
Provider.of<MyWalletsProvider>(context);
2021-04-03 00:07:03 +02:00
WalletOptionsProvider _walletOptions =
Provider.of<WalletOptionsProvider>(context);
final bool isWalletsExists = _myWalletProvider.checkIfWalletExist();
if (!isWalletsExists) {
2021-11-14 19:21:20 +01:00
return const Text('');
}
2021-03-31 02:38:44 +02:00
if (_myWalletProvider.listWallets.isEmpty) {
return Expanded(
2021-11-14 19:21:20 +01:00
child: Column(children: const <Widget>[
Center(
child: Text(
'Veuillez générer votre premier portefeuille',
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
)),
]));
}
2021-03-31 02:38:44 +02:00
List _listWallets = _myWalletProvider.listWallets;
WalletData defaultWallet =
_myWalletProvider.getDefaultWallet(configBox.get('currentChest'));
2021-11-09 04:22:04 +01:00
return CustomScrollView(slivers: <Widget>[
2021-11-14 19:21:20 +01:00
const SliverToBoxAdapter(child: SizedBox(height: 20)),
2021-11-09 04:22:04 +01:00
SliverGrid.count(
2021-11-14 19:21:20 +01:00
key: const Key('listWallets'),
2021-11-09 04:22:04 +01:00
crossAxisCount: 2,
childAspectRatio: 1,
crossAxisSpacing: 0,
mainAxisSpacing: 0,
children: <Widget>[
for (WalletData _repository in _listWallets)
Padding(
2021-11-14 19:21:20 +01:00
padding: const EdgeInsets.all(16),
child: GestureDetector(
onTap: () async {
await _walletOptions.readLocalWallet(
context,
_repository,
_myWalletProvider.pinCode,
_myWalletProvider.pinLenght);
Navigator.push(
context,
SmoothTransition(
2021-04-03 00:07:03 +02:00
page: WalletOptions(
wallet: _repository,
)));
// Navigator.push(context,
// MaterialPageRoute(builder: (context) {
// return UnlockingWallet(wallet: _repository);
// }));
},
child: ClipOvalShadow(
2021-11-14 19:21:20 +01:00
shadow: const Shadow(
color: Colors.transparent,
offset: Offset(0, 0),
blurRadius: 5,
),
clipper: CustomClipperOval(),
child: ClipRRect(
2021-11-14 19:21:20 +01:00
borderRadius:
const BorderRadius.all(Radius.circular(12)),
child: Column(children: <Widget>[
Expanded(
child: Container(
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(
gradient: RadialGradient(
radius: 0.6,
colors: [
Colors.green[400],
2021-11-14 19:21:20 +01:00
const Color(0xFFE7E7A6),
],
)),
child:
// SvgPicture.asset('assets/chopp-gecko2.png',
// semanticsLabel: 'Gecko', height: 48),
Image.asset(
'assets/avatars/${_repository.imageName}',
alignment: Alignment.bottomCenter,
scale: 0.5,
2021-11-09 04:22:04 +01:00
),
)),
2021-11-11 22:38:28 +01:00
// balanceBuilder(context, _walletOptions.pubkey.text),
ListTile(
2021-11-14 19:21:20 +01:00
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(12))),
// contentPadding: const EdgeInsets.only(left: 7.0),
tileColor:
_repository.id()[1] == defaultWallet.id()[1]
? orangeC
2021-11-14 19:21:20 +01:00
: const Color(0xffFFD58D),
// leading: Text('IMAGE'),
// subtitle: Text(_repository.split(':')[3],
// style: TextStyle(fontSize: 12.0, fontFamily: 'Monospace')),
title: Center(
2021-11-11 22:38:28 +01:00
child: Padding(
2021-11-14 19:21:20 +01:00
padding:
const EdgeInsets.symmetric(horizontal: 5),
2021-11-11 22:38:28 +01:00
child: Text(
2021-11-14 19:21:20 +01:00
_repository.name,
2021-11-11 22:38:28 +01:00
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 17.0,
color: _repository.id()[1] ==
defaultWallet.id()[1]
2021-11-14 19:21:20 +01:00
? const Color(0xffF9F9F1)
2021-11-11 22:38:28 +01:00
: Colors.black,
fontStyle: FontStyle.italic),
),
),
),
// dense: true,
onTap: () {
Navigator.push(
context,
SmoothTransition(
page: WalletOptions(
wallet: _repository,
),
),
);
},
)
]),
),
),
)),
addNewDerivation(context),
// SizedBox(height: 1),
// Padding(
// padding: EdgeInsets.symmetric(horizontal: 35),
// child: Text(
// 'Ajouter un portefeuille',
// textAlign: TextAlign.center,
// style: TextStyle(fontSize: 18),
// ))
2021-11-09 04:22:04 +01:00
]),
// SliverToBoxAdapter(child: Spacer()),
SliverToBoxAdapter(child: chestOptions(context)),
2021-11-09 04:22:04 +01:00
]);
}
2021-04-03 00:07:03 +02:00
2021-11-11 22:38:28 +01:00
Widget balanceBuilder(context, String _pubkey) {
return Query(
options: QueryOptions(
document: gql(getBalance),
variables: {
'pubkey': _pubkey,
},
// pollInterval: Duration(seconds: 1),
),
builder: (QueryResult result,
{VoidCallback refetch, FetchMore fetchMore}) {
if (result.hasException) {
return Text(result.exception.toString());
}
if (result.isLoading) {
2021-11-14 19:21:20 +01:00
return const Text('Loading');
2021-11-11 22:38:28 +01:00
}
String wBalanceUD;
if (result.data['balance'] == null) {
wBalanceUD = '0.0';
} else {
int wBalanceG1 = result.data['balance']['amount'];
int currentUD = result.data['currentUd']['amount'];
double wBalanceUDBrut = wBalanceG1 / currentUD; // .toString();
wBalanceUD =
double.parse((wBalanceUDBrut).toStringAsFixed(2)).toString();
}
return Text(wBalanceUD);
});
}
2021-11-09 04:22:04 +01:00
Widget addNewDerivation(context) {
return Padding(
2021-11-14 19:21:20 +01:00
padding: const EdgeInsets.all(16),
2021-11-09 04:22:04 +01:00
child: ClipRRect(
2021-11-14 19:21:20 +01:00
borderRadius: const BorderRadius.all(Radius.circular(12)),
2021-11-09 04:22:04 +01:00
child: Column(children: <Widget>[
Expanded(
child: InkWell(
2021-11-14 19:21:20 +01:00
key: const Key('addDerivation'),
2021-11-09 04:22:04 +01:00
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return addNewDerivationPopup(context);
});
},
2021-11-09 04:22:04 +01:00
child: Container(
width: double.infinity,
height: double.infinity,
decoration: BoxDecoration(color: floattingYellow),
2021-11-14 19:21:20 +01:00
child: const Center(
2021-11-09 04:22:04 +01:00
child: Text(
'+',
style: TextStyle(
fontSize: 150,
fontWeight: FontWeight.w700,
color: Color(0xFFFCB437)),
)),
)),
),
2021-11-09 04:22:04 +01:00
])));
}
2021-11-09 04:22:04 +01:00
Widget addNewDerivationPopup(context) {
final TextEditingController _newDerivationName = TextEditingController();
2021-02-15 01:44:25 +01:00
MyWalletsProvider _myWalletProvider =
Provider.of<MyWalletsProvider>(context);
2021-02-15 01:44:25 +01:00
return AlertDialog(
content: Stack(
clipBehavior: Clip.hardEdge,
2021-02-15 01:44:25 +01:00
children: <Widget>[
Form(
key: _derivationKey,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
2021-11-14 19:21:20 +01:00
const Text('Nom du portefeuille:'),
2021-02-15 01:44:25 +01:00
Padding(
2021-11-14 19:21:20 +01:00
padding: const EdgeInsets.all(8.0),
2021-02-15 01:44:25 +01:00
child: TextFormField(
2021-11-14 19:21:20 +01:00
key: const Key('DerivationNameKey'),
2021-02-15 01:44:25 +01:00
controller: _newDerivationName,
textAlign: TextAlign.center,
autofocus: true,
),
),
2021-11-14 19:21:20 +01:00
const SizedBox(height: 20),
2021-02-15 01:44:25 +01:00
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
2021-11-14 19:21:20 +01:00
key: const Key('validDerivation'),
style: ElevatedButton.styleFrom(
elevation: 1,
2021-11-08 23:12:25 +01:00
primary: yellowC, // background
onPrimary: Colors.black, // foreground
),
onPressed: () async {
await _myWalletProvider
.generateNewDerivation(
context, _newDerivationName.text)
.then((_) => _newDerivationName.text == '');
},
2021-11-14 19:21:20 +01:00
child: const Text("Créer")),
2021-02-15 01:44:25 +01:00
)
],
),
),
],
),
);
}
}
// extension Range on num {
// bool isBetween(num from, num to) {
// return from < this && this < to;
// }
// }
class CustomClipperOval extends CustomClipper<Rect> {
@override
Rect getClip(Size size) {
return Rect.fromCircle(
2021-11-14 19:21:20 +01:00
center: Offset(size.width / 2, size.width / 2),
radius: size.width / 2 + 3);
}
@override
bool shouldReclip(CustomClipper<Rect> oldClipper) {
return false;
}
}
class ClipOvalShadow extends StatelessWidget {
final Shadow shadow;
final CustomClipper<Rect> clipper;
final Widget child;
2021-11-14 19:21:20 +01:00
const ClipOvalShadow({
Key key,
@required this.shadow,
@required this.clipper,
@required this.child,
2021-11-14 19:21:20 +01:00
}) : super(key: key);
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: _ClipOvalShadowPainter(
2021-11-14 19:21:20 +01:00
clipper: clipper,
shadow: shadow,
),
2021-11-14 19:21:20 +01:00
child: ClipRect(child: child, clipper: clipper),
);
}
}
class _ClipOvalShadowPainter extends CustomPainter {
final Shadow shadow;
final CustomClipper<Rect> clipper;
_ClipOvalShadowPainter({@required this.shadow, @required this.clipper});
@override
void paint(Canvas canvas, Size size) {
var paint = shadow.toPaint();
2021-11-14 19:21:20 +01:00
var clipRect = clipper.getClip(size).shift(const Offset(0, 0));
canvas.drawOval(clipRect, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return true;
}
}