2021-07-12 04:32:49 -07:00
|
|
|
import 'package:barcode_scan/barcode_scan.dart';
|
2021-06-26 07:30:12 -07:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/rendering.dart';
|
|
|
|
import 'package:warp_api/warp_api.dart';
|
2021-07-12 04:32:49 -07:00
|
|
|
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
|
2021-06-26 07:30:12 -07:00
|
|
|
|
|
|
|
import 'main.dart';
|
|
|
|
|
|
|
|
class RestorePage extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _RestorePageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _RestorePageState extends State<RestorePage> {
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
final _keyController = TextEditingController();
|
|
|
|
final _nameController = TextEditingController();
|
|
|
|
var _validKey = true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text("\u24E9 Wallet"),
|
|
|
|
),
|
|
|
|
body: Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: Padding(
|
2021-07-12 04:32:49 -07:00
|
|
|
padding: EdgeInsets.all(8),
|
2021-06-26 07:30:12 -07:00
|
|
|
child: Column(children: [
|
|
|
|
TextFormField(
|
|
|
|
decoration: InputDecoration(labelText: 'Account Name'),
|
|
|
|
controller: _nameController,
|
|
|
|
validator: (String name) {
|
|
|
|
if (name == null || name.isEmpty)
|
|
|
|
return "Account name is required";
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
),
|
2021-07-12 04:32:49 -07:00
|
|
|
Row(
|
|
|
|
// padding: EdgeInsets.all(8),
|
|
|
|
// decoration: BoxDecoration(
|
|
|
|
// border: Border.all(width: 2.0),
|
|
|
|
// ),
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: TextFormField(
|
|
|
|
decoration: InputDecoration(
|
|
|
|
labelText: 'Key',
|
|
|
|
hintText:
|
|
|
|
'Enter Seed, Secret Key or Viewing Key. Leave blank for a new account'),
|
|
|
|
minLines: 4,
|
|
|
|
maxLines: 4,
|
|
|
|
controller: _keyController,
|
|
|
|
onChanged: _checkKey,
|
|
|
|
)),
|
|
|
|
IconButton(
|
|
|
|
icon: new Icon(MdiIcons.qrcodeScan), onPressed: _onScan)
|
|
|
|
],
|
2021-06-26 07:30:12 -07:00
|
|
|
),
|
|
|
|
ButtonBar(children: [
|
|
|
|
ElevatedButton(onPressed: _onCancel, child: Text('Cancel')),
|
|
|
|
ElevatedButton(
|
2021-07-12 04:32:49 -07:00
|
|
|
onPressed: _validKey ? _onOK : null, child: Text('OK')),
|
2021-06-26 07:30:12 -07:00
|
|
|
])
|
|
|
|
]))));
|
|
|
|
}
|
|
|
|
|
|
|
|
_onOK() async {
|
|
|
|
if (_formKey.currentState.validate()) {
|
2021-07-12 04:32:49 -07:00
|
|
|
final account =
|
|
|
|
WarpApi.newAccount(_nameController.text, _keyController.text);
|
2021-06-26 07:30:12 -07:00
|
|
|
await accountManager.refresh();
|
2021-07-09 06:33:39 -07:00
|
|
|
await accountManager.setActiveAccountId(account);
|
2021-06-26 07:30:12 -07:00
|
|
|
if (accountManager.accounts.length == 1) {
|
|
|
|
if (_keyController.text == "")
|
|
|
|
WarpApi.skipToLastHeight(); // single new account -> quick sync
|
2021-07-07 08:40:05 -07:00
|
|
|
else {
|
|
|
|
final snackBar = SnackBar(content: Text("Scan starting momentarily"));
|
|
|
|
rootScaffoldMessengerKey.currentState.showSnackBar(snackBar);
|
2021-06-26 07:30:12 -07:00
|
|
|
WarpApi.rewindToHeight(0);
|
2021-07-07 08:40:05 -07:00
|
|
|
}
|
2021-06-26 07:30:12 -07:00
|
|
|
}
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
Navigator.of(context).pushReplacementNamed('/account');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_onCancel() {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
_checkKey(key) {
|
|
|
|
setState(() {
|
|
|
|
_validKey = key == "" || WarpApi.validKey(key);
|
|
|
|
});
|
|
|
|
}
|
2021-07-12 04:32:49 -07:00
|
|
|
|
|
|
|
void _onScan() async {
|
|
|
|
var code = await BarcodeScanner.scan();
|
|
|
|
setState(() {
|
|
|
|
final key = code.rawContent;
|
|
|
|
_keyController.text = key;
|
|
|
|
_validKey = key == "" || WarpApi.validKey(key);
|
|
|
|
});
|
|
|
|
}
|
2021-06-26 07:30:12 -07:00
|
|
|
}
|