zwallet/lib/restore.dart

90 lines
2.9 KiB
Dart
Raw Normal View History

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';
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(
padding: EdgeInsets.all(4),
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;
},
),
Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
border: Border.all(width: 2.0),
),
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,
),
),
ButtonBar(children: [
ElevatedButton(onPressed: _onCancel, child: Text('Cancel')),
ElevatedButton(
onPressed: _validKey ? _onOK : null,
child: Text('OK')),
])
]))));
}
_onOK() async {
if (_formKey.currentState.validate()) {
final account = WarpApi.newAccount(
_nameController.text, _keyController.text);
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);
});
}
}