gecko/lib/screens/myWallets/wallets_home.dart

631 lines
23 KiB
Dart
Raw Normal View History

2022-09-12 07:26:13 +02:00
// ignore_for_file: use_build_context_synchronously
import 'dart:io';
2022-06-17 01:13:14 +02:00
import 'package:easy_localization/easy_localization.dart';
2022-09-12 07:26:13 +02:00
import 'package:flutter_markdown/flutter_markdown.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/widgets_keys.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:flutter/material.dart';
import 'package:gecko/providers/substrate_sdk.dart';
2021-12-24 15:27:38 +01:00
import 'package:gecko/providers/wallet_options.dart';
import 'package:gecko/screens/myWallets/chest_options.dart';
import 'package:gecko/screens/myWallets/import_g1_v1.dart';
import 'package:gecko/screens/myWallets/unlocking_wallet.dart';
2021-11-14 19:21:20 +01:00
import 'package:gecko/screens/myWallets/wallet_options.dart';
import 'package:gecko/widgets/balance.dart';
import 'package:gecko/widgets/bottom_app_bar.dart';
2022-12-10 08:16:38 +01:00
import 'package:gecko/widgets/commons/offline_info.dart';
import 'package:gecko/widgets/commons/smooth_transition.dart';
import 'package:gecko/widgets/name_by_address.dart';
2023-05-24 16:27:13 +02:00
import 'package:gecko/widgets/payment_popup.dart';
import 'package:provider/provider.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:tutorial_coach_mark/tutorial_coach_mark.dart';
class WalletsHome extends StatefulWidget {
2021-12-23 12:36:09 +01:00
const WalletsHome({Key? key}) : super(key: key);
2021-11-14 19:21:20 +01:00
@override
State<WalletsHome> createState() => _WalletsHomeState();
}
class _WalletsHomeState extends State<WalletsHome> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
final myWalletProvider = Provider.of<MyWalletsProvider>(context);
final currentChestNumber = myWalletProvider.getCurrentChest();
final ChestData currentChest = chestBox.get(currentChestNumber)!;
2021-12-01 08:14:07 +01:00
myWalletProvider.listWallets =
myWalletProvider.readAllWallets(currentChestNumber);
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(
2022-05-29 00:00:57 +02:00
backgroundColor: backgroundColor,
2021-11-09 04:22:04 +01:00
appBar: AppBar(
elevation: 1,
2021-11-15 04:16:25 +01:00
toolbarHeight: 60 * ratio,
2021-11-09 04:22:04 +01:00
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('/'),
);
}),
2022-12-08 09:51:08 +01:00
title: Row(
children: [
Image.asset(
'assets/chests/${currentChest.imageName}',
height: 32,
2022-12-08 09:51:08 +01:00
),
const SizedBox(width: 17),
Text(currentChest.name!,
style: TextStyle(color: Colors.grey[850])),
],
),
2021-11-14 19:21:20 +01:00
backgroundColor: const Color(0xffFFD58D),
2021-11-09 04:22:04 +01:00
),
2022-09-12 07:26:13 +02:00
bottomNavigationBar: myWalletProvider.lastFlyBy == ''
2022-12-08 05:26:55 +01:00
? const GeckoBottomAppBar(
actualRoute: 'safeHome',
)
2022-09-12 07:26:13 +02:00
: dragInfo(context),
2021-11-09 04:22:04 +01:00
body: SafeArea(
child: Stack(children: [
myWalletsTiles(context, currentChestNumber),
2022-12-10 08:16:38 +01:00
const OfflineInfo(),
]),
2021-11-09 04:22:04 +01:00
),
),
);
}
2022-09-12 07:26:13 +02:00
Widget dragInfo(BuildContext context) {
final myWalletProvider =
Provider.of<MyWalletsProvider>(context, listen: false);
final walletDataFrom =
myWalletProvider.getWalletDataByAddress(myWalletProvider.dragAddress);
final walletDataTo =
myWalletProvider.getWalletDataByAddress(myWalletProvider.lastFlyBy);
final bool isSameAddress =
myWalletProvider.dragAddress == myWalletProvider.lastFlyBy;
final screenWidth = MediaQuery.of(homeContext).size.width;
2022-09-12 07:26:13 +02:00
return Container(
color: yellowC,
width: screenWidth,
height: 80,
child: Center(
child: Column(
children: [
const SizedBox(height: 5),
Text('${'executeATransfer'.tr()}:'),
MarkdownBody(data: '${'from'.tr()} **${walletDataFrom!.name}**'),
if (isSameAddress) Text('chooseATargetWallet'.tr()),
if (!isSameAddress)
MarkdownBody(data: 'Vers: **${walletDataTo!.name}**'),
],
)),
);
}
Widget chestOptions(BuildContext context, final myWalletProvider) {
2021-11-09 04:22:04 +01:00
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: 80,
2021-12-23 21:44:24 +01:00
width: 420,
child: ElevatedButton.icon(
icon: Image.asset(
'assets/chests/config.png',
2021-12-23 21:44:24 +01:00
height: 60,
),
style: ElevatedButton.styleFrom(
2022-09-05 08:12:24 +02:00
foregroundColor: Colors.black, elevation: 2,
backgroundColor: floattingYellow, // foreground
),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return ChestOptions(walletProvider: myWalletProvider);
}),
),
2022-06-17 01:13:14 +02:00
label: Text(
" ${"manageChest".tr()}",
2022-06-17 01:13:14 +02:00
style: const TextStyle(
2021-12-23 21:44:24 +01:00
fontSize: 22,
fontWeight: FontWeight.w700,
color: Color(0xff8a3c0f),
),
),
)),
2021-11-14 19:21:20 +01:00
const SizedBox(height: 30),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SvgPicture.asset(
'assets/cesium_bw2.svg',
semanticsLabel: 'CS',
height: 50,
),
const SizedBox(width: 5),
InkWell(
key: keyImportG1v1,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return const ImportG1v1();
}),
);
},
child: SizedBox(
width: 350,
height: 60,
child: Center(
child: Text('importG1v1'.tr(),
style: TextStyle(
fontSize: 22,
color: Colors.blue[900],
fontWeight: FontWeight.w500))),
),
),
],
2022-08-20 20:40:33 +02:00
),
const SizedBox(height: 20),
2022-06-01 21:00:17 +02:00
InkWell(
key: keyChangeChest,
2023-02-03 11:47:55 +01:00
onTap: () {
2023-02-19 19:35:11 +01:00
// Navigator.push(
// context,
// MaterialPageRoute(builder: (context) {
// return const ChooseChest();
// }),
// );
},
2022-06-01 21:00:17 +02:00
child: SizedBox(
width: 400,
2022-08-20 20:40:33 +02:00
height: 60,
2022-06-01 21:00:17 +02:00
child: Center(
2022-06-17 01:13:14 +02:00
child: Text('changeChest'.tr(),
style: const TextStyle(
2022-06-01 21:00:17 +02:00
fontSize: 22,
2023-02-19 19:35:11 +01:00
color: Colors.grey, //orangeC
2022-06-01 21:00:17 +02:00
fontWeight: FontWeight.w500))),
),
),
2021-11-14 19:21:20 +01:00
const SizedBox(height: 30)
2021-11-09 04:22:04 +01:00
]);
}
Widget myWalletsTiles(BuildContext context, int currentChestNumber) {
final myWalletProvider = Provider.of<MyWalletsProvider>(context);
final walletOptions =
2021-12-24 15:27:38 +01:00
Provider.of<WalletOptionsProvider>(context, listen: false);
final bool isWalletsExists = myWalletProvider.checkIfWalletExist();
final sub = Provider.of<SubstrateSdk>(context, listen: false);
if (!isWalletsExists) {
2021-11-14 19:21:20 +01:00
return const Text('');
}
if (myWalletProvider.listWallets.isEmpty) {
2023-05-24 14:37:28 +02:00
return const Expanded(
child: Column(children: <Widget>[
Center(
child: Text(
'Veuillez générer votre premier portefeuille',
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
)),
]));
}
// Get wallet list and sort by derivation number
List<WalletData> listWallets = myWalletProvider.listWallets;
listWallets.sort((p1, p2) {
return Comparable.compare(p1.number!, p2.number!);
});
WalletData? defaultWallet = myWalletProvider.getDefaultWallet();
final screenWidth = MediaQuery.of(context).size.width;
2022-12-23 01:58:44 +01:00
int nTule;
2021-12-23 21:44:24 +01:00
if (screenWidth >= 900) {
nTule = 4;
} else if (screenWidth >= 650) {
nTule = 3;
2022-12-23 01:58:44 +01:00
} else {
nTule = 2;
2021-12-23 21:44:24 +01:00
}
final tutorialCoachMark = TutorialCoachMark(
targets: [
TargetFocus(
identify: "drag_and_drop",
keyTarget: keyDragAndDrop,
contents: [
TargetContent(
child: Column(
children: [
Image.asset('assets/drag-and-drop.png', height: 140),
const SizedBox(height: 15),
Text(
'explainDraggableWallet'.tr(),
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 22, fontWeight: FontWeight.w500),
),
],
))
],
alignSkip: Alignment.bottomRight,
enableOverlayTab: true,
),
],
colorShadow: orangeC,
textSkip: "skip".tr(),
paddingFocus: 10,
opacityShadow: 0.8,
);
// configBox.delete('showDraggableTutorial');
final bool showDraggableTutorial =
configBox.get('showDraggableTutorial') ?? true;
if (listWallets.length > 1 && showDraggableTutorial) {
tutorialCoachMark.show(context: context);
configBox.put('showDraggableTutorial', false);
}
2022-12-23 01:58:44 +01:00
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(
key: keyListWallets,
2021-12-23 21:44:24 +01:00
crossAxisCount: nTule,
2021-11-09 04:22:04 +01:00
childAspectRatio: 1,
crossAxisSpacing: 0,
mainAxisSpacing: 0,
children: <Widget>[
for (WalletData repository in listWallets)
LongPressDraggable<String>(
2022-09-12 07:26:13 +02:00
delay: const Duration(milliseconds: 200),
data: repository.address,
2022-11-16 13:39:42 +01:00
dragAnchorStrategy:
(Draggable<Object> _, BuildContext __, Offset ___) =>
const Offset(0, 0),
// feedbackOffset: const Offset(-500, -500),
// dragAnchorStrategy: childDragAnchorStrategy,
2022-09-12 07:26:13 +02:00
onDragStarted: () =>
myWalletProvider.dragAddress = repository.address,
2022-09-12 07:26:13 +02:00
onDragEnd: (_) {
myWalletProvider.lastFlyBy = '';
myWalletProvider.dragAddress = '';
myWalletProvider.reload();
},
feedback: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
backgroundColor: orangeC,
shape: const CircleBorder(),
padding: const EdgeInsets.all(15),
),
child: const SizedBox(
height: 35,
child: Image(image: AssetImage('assets/vector_white.png')),
),
),
2022-09-12 07:26:13 +02:00
child: DragTarget<String>(
onAccept: (senderAddress) async {
log.d(
'INTERPAY: sender: $senderAddress --- receiver: ${repository.address}');
2022-09-12 07:26:13 +02:00
final walletData = myWalletProvider
.getWalletDataByAddress(senderAddress);
await sub.setCurrentWallet(walletData!);
sub.reload();
2023-04-12 03:10:02 +02:00
paymentPopup(
context,
repository.address,
g1WalletsBox.get(repository.address)!.username ??
repository.name!);
2022-09-12 07:26:13 +02:00
},
onMove: (details) {
if (repository.address != myWalletProvider.lastFlyBy) {
myWalletProvider.lastFlyBy = repository.address;
2022-09-12 07:26:13 +02:00
myWalletProvider.reload();
}
},
onWillAccept: (senderAddress) =>
senderAddress != repository.address,
2022-09-12 07:26:13 +02:00
builder: (
BuildContext context,
List<dynamic> accepted,
List<dynamic> rejected,
) {
return Padding(
padding: const EdgeInsets.all(16),
child: GestureDetector(
key: keyOpenWallet(repository.address),
2022-09-12 07:26:13 +02:00
onTap: () {
walletOptions.getAddress(
currentChestNumber, repository.derivation!);
Navigator.push(
context,
SmoothTransition(
page: WalletOptions(
wallet: repository,
),
),
2022-09-12 07:26:13 +02:00
);
},
child: SizedBox(
key: repository.number == 1
? keyDragAndDrop
: const Key('nothing'),
child: ClipOvalShadow(
shadow: const Shadow(
color: Colors.transparent,
offset: Offset(0, 0),
blurRadius: 5,
),
clipper: CustomClipperOval(),
child: ClipRRect(
borderRadius:
const BorderRadius.all(Radius.circular(12)),
child: Column(children: <Widget>[
Expanded(
child: Container(
width: double.infinity,
height: double.infinity,
decoration: const BoxDecoration(
gradient: RadialGradient(
radius: 0.8,
colors: [
Color.fromARGB(255, 255, 255, 211),
yellowC,
],
),
2022-12-08 05:26:55 +01:00
),
child:
// SvgPicture.asset('assets/chopp-gecko2.png',
// semanticsLabel: 'Gecko', height: 48),
repository.imageCustomPath == null ||
repository.imageCustomPath == ''
? Image.asset(
'assets/avatars/${repository.imageDefaultPath}',
alignment:
Alignment.bottomCenter,
scale: 0.5,
)
: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.transparent,
image: DecorationImage(
fit: BoxFit.fitHeight,
image: FileImage(
File(repository
.imageCustomPath!),
),
2022-09-12 07:26:13 +02:00
),
),
),
)),
Stack(children: <Widget>[
BalanceBuilder(
address: repository.address,
isDefault: repository.address ==
defaultWallet.address),
Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
Column(
children: [
const SizedBox(height: 7),
Opacity(
opacity: 0.7,
child: NameByAddress(
wallet: repository,
size: 20,
color:
defaultWallet.address ==
repository.address
? Colors.white
: Colors.black,
fontWeight: FontWeight.w600,
fontStyle: FontStyle.normal,
))
],
),
],
),
]),
2022-09-12 07:26:13 +02:00
]),
),
2022-09-12 07:26:13 +02:00
),
),
),
2022-09-12 07:26:13 +02:00
);
}),
),
Consumer<SubstrateSdk>(builder: (context, sub, _) {
return sub.nodeConnected
? addNewDerivation(context)
: const Text('');
}),
2021-11-09 04:22:04 +01:00
]),
SliverToBoxAdapter(child: chestOptions(context, myWalletProvider)),
2021-11-09 04:22:04 +01:00
]);
}
2021-04-03 00:07:03 +02:00
2021-11-09 04:22:04 +01:00
Widget addNewDerivation(context) {
final myWalletProvider = Provider.of<MyWalletsProvider>(context);
String newDerivationName =
'${'wallet'.tr()} ${myWalletProvider.listWallets.last.number! + 2}';
2021-11-09 04:22:04 +01:00
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(
key: keyAddDerivation,
onTap: () async {
if (!myWalletProvider.isNewDerivationLoading) {
WalletData? defaultWallet =
myWalletProvider.getDefaultWallet();
String? pin;
if (myWalletProvider.pinCode == '') {
pin = await Navigator.push(
context,
MaterialPageRoute(
builder: (homeContext) {
return UnlockingWallet(wallet: defaultWallet);
},
),
);
}
if (pin != null || myWalletProvider.pinCode != '') {
await myWalletProvider.generateNewDerivation(
context, newDerivationName);
}
}
},
2021-11-09 04:22:04 +01:00
child: Container(
width: double.infinity,
height: double.infinity,
2022-09-12 12:38:32 +02:00
decoration: const BoxDecoration(color: floattingYellow),
child: Center(
child: myWalletProvider.isNewDerivationLoading
2022-09-12 12:38:32 +02:00
? const SizedBox(
height: 60,
width: 60,
child: CircularProgressIndicator(
color: orangeC,
strokeWidth: 7,
),
)
: const 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
])));
}
}
2022-12-23 01:58:44 +01:00
class BalanceBuilder extends StatelessWidget {
const BalanceBuilder({
Key? key,
required this.address,
required this.isDefault,
}) : super(key: key);
final String address;
final bool isDefault;
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
color: isDefault ? orangeC : yellowC,
child: Padding(
padding:
const EdgeInsets.only(left: 5, right: 5, top: 38, bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Opacity(
opacity: 0.7,
child: Balance(
address: address,
size: 16,
color: isDefault ? Colors.white : Colors.black,
loadingColor: isDefault ? yellowC : orangeC),
)
],
)),
);
}
}
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({
2021-12-23 12:36:09 +01:00
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,
),
child: ClipRect(clipper: clipper, child: child),
);
}
}
class _ClipOvalShadowPainter extends CustomPainter {
final Shadow shadow;
final CustomClipper<Rect> clipper;
2021-12-23 12:36:09 +01:00
_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;
}
}