gecko/lib/screens/myWallets/show_seed.dart

329 lines
12 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:typed_data';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/services.dart';
import 'package:gecko/globals.dart';
import 'package:flutter/material.dart';
import 'package:gecko/models/wallet_data.dart';
import 'package:gecko/providers/my_wallets.dart';
import 'package:gecko/providers/substrate_sdk.dart';
import 'package:gecko/screens/common_elements.dart';
import 'package:pdf/pdf.dart';
import 'package:printing/printing.dart';
import 'package:provider/provider.dart';
import 'package:pdf/widgets.dart' as pw;
class ShowSeed extends StatelessWidget {
const ShowSeed(
{Key? keyMyWallets,
required this.walletName,
required this.walletProvider})
: super(key: keyMyWallets);
final String? walletName;
final MyWalletsProvider walletProvider;
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
MyWalletsProvider _myWalletProvider =
Provider.of<MyWalletsProvider>(context, listen: false);
CommonElements common = CommonElements();
SubstrateSdk _sub = Provider.of<SubstrateSdk>(context, listen: false);
WalletData defaultWallet = _myWalletProvider.getDefaultWallet();
return Scaffold(
backgroundColor: backgroundColor,
appBar: AppBar(
toolbarHeight: 60 * ratio,
title: const SizedBox(
height: 22,
child: Text('Ma phrase de restauration'),
)),
body: SafeArea(
child: Column(children: <Widget>[
const Spacer(flex: 1),
FutureBuilder(
future: _sub.getSeed(
defaultWallet.address!, walletProvider.pinCode),
builder: (BuildContext context, AsyncSnapshot<String?> _seed) {
if (_seed.connectionState != ConnectionState.done ||
_seed.hasError) {
return SizedBox(
height: 15,
width: 15,
child: CircularProgressIndicator(
color: orangeC,
strokeWidth: 2,
),
);
} else if (!_seed.hasData) {
return const Text('');
}
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Column(children: [
common.buildText(
'Tâchez de garder cette phrase bien secrète, car elle permet à quiconque la connaît daccéder à tous vos portefeuilles.'),
SizedBox(height: 35 * ratio),
sentanceArray(context, _seed.data!.split(' ')),
const SizedBox(height: 20),
SizedBox(
height: 40,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
elevation: 1,
primary: orangeC, // background
onPrimary: Colors.black, // foreground
),
onPressed: () {
Clipboard.setData(
ClipboardData(text: _seed.data));
snackCopyKey(context);
},
child: Row(children: <Widget>[
Image.asset(
'assets/walletOptions/copy-white.png',
height: 25,
),
const SizedBox(width: 7),
Text(
'copy'.tr(),
style: TextStyle(
fontSize: 15, color: Colors.grey[50]),
)
]),
),
),
const SizedBox(height: 30),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) {
return PrintWallet(_seed.data);
}),
);
},
child: Image.asset(
'assets/printer.png',
height: 42 * ratio,
),
),
]),
]);
}),
const Spacer(flex: 2),
SizedBox(
width: 380 * ratio,
height: 60 * ratio,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
elevation: 4,
primary: orangeC, // background
onPrimary: Colors.white, // foreground
),
onPressed: () {
Navigator.pop(context);
},
child: Text(
'Fermer',
style: TextStyle(
fontSize: 23 * ratio, fontWeight: FontWeight.w600),
),
),
),
const Spacer(flex: 2),
]),
));
}
snackCopyKey(context) {
const snackBar = SnackBar(
padding: EdgeInsets.all(20),
content: Text(
"Votre phrase de restauration a été copié dans votre presse-papier.",
style: TextStyle(fontSize: 16)),
duration: Duration(seconds: 2));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
Widget sentanceArray(BuildContext context, List _mnemonic) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 3),
child: Container(
constraints: const BoxConstraints(maxWidth: 450),
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
color: const Color(0xffeeeedd),
borderRadius: const BorderRadius.all(
Radius.circular(10),
)),
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Row(children: <Widget>[
arrayCell(_mnemonic[0], 1),
arrayCell(_mnemonic[1], 2),
arrayCell(_mnemonic[2], 3),
arrayCell(_mnemonic[3], 4),
]),
const SizedBox(height: 15),
Row(children: <Widget>[
arrayCell(_mnemonic[4], 5),
arrayCell(_mnemonic[5], 6),
arrayCell(_mnemonic[6], 7),
arrayCell(_mnemonic[7], 8),
]),
const SizedBox(height: 15),
Row(children: <Widget>[
arrayCell(_mnemonic[8], 9),
arrayCell(_mnemonic[9], 10),
arrayCell(_mnemonic[10], 11),
arrayCell(_mnemonic[11], 12),
]),
])),
);
}
Widget arrayCell(dataWord, int nbr) {
log.d(nbr);
return SizedBox(
width: 100,
child: Column(children: <Widget>[
Text(
nbr.toString(),
style:
TextStyle(fontSize: 13 * ratio, color: const Color(0xff6b6b52)),
),
Text(
dataWord,
key: Key('word$dataWord'),
style: TextStyle(fontSize: 17 * ratio, color: Colors.black),
),
]),
);
}
}
class PrintWallet extends StatelessWidget {
const PrintWallet(this.sentence, {Key? key}) : super(key: key);
final String? sentence;
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.black),
onPressed: () {
Navigator.pop(context);
}),
backgroundColor: yellowC,
foregroundColor: Colors.black,
toolbarHeight: 60 * ratio,
title: const SizedBox(
height: 22,
child: Text(
'Imprimer ma phrase de restauration',
style: TextStyle(fontWeight: FontWeight.w600),
),
),
),
body: PdfPreview(
canDebug: false,
canChangeOrientation: false,
build: (format) => printWallet(sentence!),
),
),
);
}
Future<Uint8List> printWallet(String _seed) async {
final ByteData fontData =
await rootBundle.load("assets/OpenSans-Regular.ttf");
final pw.Font ttf = pw.Font.ttf(fontData.buffer.asByteData());
final pdf = pw.Document();
int nbr = 1;
final _seedList = _seed.split(' ');
// const imageProvider = AssetImage('assets/icon/gecko_final.png');
// final geckoLogo = await flutterImageProvider(imageProvider);
pw.Widget arrayCell(String dataWord, int _nbr) {
nbr++;
return pw.SizedBox(
width: 120,
child: pw.Column(children: <pw.Widget>[
pw.Text(
_nbr.toString(),
style: pw.TextStyle(
fontSize: 15, color: const PdfColor(0.5, 0, 0), font: ttf),
),
pw.Text(
dataWord,
style: pw.TextStyle(
fontSize: 20, color: const PdfColor(0, 0, 0), font: ttf),
),
pw.SizedBox(height: 10)
]),
);
}
pdf.addPage(
pw.Page(
pageFormat: PdfPageFormat.a4,
build: (context) {
return pw.Column(
// mainAxisAlignment: pw.MainAxisAlignment.center,
// mainAxisSize: pw.MainAxisSize.max,
// crossAxisAlignment: pw.CrossAxisAlignment.center,
children: <pw.Widget>[
pw.Row(children: <pw.Widget>[
arrayCell(_seedList[0], nbr),
arrayCell(_seedList[1], nbr),
arrayCell(_seedList[2], nbr),
arrayCell(_seedList[3], nbr),
]),
pw.Row(children: <pw.Widget>[
arrayCell(_seedList[4], nbr),
arrayCell(_seedList[5], nbr),
arrayCell(_seedList[6], nbr),
arrayCell(_seedList[7], nbr),
]),
pw.Row(children: <pw.Widget>[
arrayCell(_seedList[8], nbr),
arrayCell(_seedList[9], nbr),
arrayCell(_seedList[10], nbr),
arrayCell(_seedList[11], nbr)
]),
pw.Expanded(
child: pw.Align(
alignment: pw.Alignment.bottomCenter,
child: pw.Text(
"Gardez cette feuille préciseusement, à labri des lézards indiscrets.",
style: pw.TextStyle(fontSize: 15, font: ttf),
)))
],
);
},
),
);
return pdf.save();
}
}