cake_wallet/lib/src/screens/new_wallet/new_wallet_page.dart

182 lines
6.7 KiB
Dart
Raw Normal View History

import 'package:cake_wallet/di.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/store/settings_store.dart';
2020-09-25 15:32:44 +00:00
import 'package:cake_wallet/utils/show_pop_up.dart';
2020-01-04 19:31:52 +00:00
import 'package:mobx/mobx.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:cake_wallet/generated/i18n.dart';
2020-06-20 07:10:00 +00:00
import 'package:cake_wallet/core/validator.dart';
import 'package:cake_wallet/src/widgets/seed_language_selector.dart';
2020-01-04 19:31:52 +00:00
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/widgets/primary_button.dart';
import 'package:cake_wallet/src/widgets/scollable_with_bottom_section.dart';
import 'package:cake_wallet/src/screens/seed_language/widgets/seed_language_picker.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
2020-09-21 11:50:26 +00:00
import 'package:cake_wallet/core/execution_state.dart';
2020-06-20 07:10:00 +00:00
import 'package:cake_wallet/view_model/wallet_new_vm.dart';
2020-01-04 19:31:52 +00:00
class NewWalletPage extends BasePage {
2020-06-20 07:10:00 +00:00
NewWalletPage(this._walletNewVM);
2020-01-08 12:26:34 +00:00
2020-06-20 07:10:00 +00:00
final WalletNewVM _walletNewVM;
2020-01-04 19:31:52 +00:00
2020-01-08 12:26:34 +00:00
@override
2020-01-04 19:31:52 +00:00
String get title => S.current.new_wallet;
@override
2020-06-20 07:10:00 +00:00
Widget body(BuildContext context) => WalletNameForm(_walletNewVM);
2020-01-04 19:31:52 +00:00
}
class WalletNameForm extends StatefulWidget {
2020-06-20 07:10:00 +00:00
WalletNameForm(this._walletNewVM);
final WalletNewVM _walletNewVM;
2020-01-04 19:31:52 +00:00
@override
2020-06-20 07:10:00 +00:00
_WalletNameFormState createState() => _WalletNameFormState(_walletNewVM);
2020-01-04 19:31:52 +00:00
}
class _WalletNameFormState extends State<WalletNameForm> {
2020-06-20 07:10:00 +00:00
_WalletNameFormState(this._walletNewVM);
static const aspectRatioImage = 1.22;
final walletNameImage = Image.asset('assets/images/wallet_name.png');
2020-09-21 11:50:26 +00:00
final walletNameLightImage =
Image.asset('assets/images/wallet_name_light.png');
2020-06-20 07:10:00 +00:00
final _formKey = GlobalKey<FormState>();
final _languageSelectorKey = GlobalKey<SeedLanguageSelectorState>();
ReactionDisposer _stateReaction;
final WalletNewVM _walletNewVM;
@override
2020-06-20 07:10:00 +00:00
void initState() {
_stateReaction ??=
2020-09-21 11:50:26 +00:00
reaction((_) => _walletNewVM.state, (ExecutionState state) {
if (state is ExecutedSuccessfullyState) {
Navigator.of(context).pushNamed(Routes.seed, arguments: true);
2020-01-04 19:31:52 +00:00
}
2020-09-21 11:50:26 +00:00
if (state is FailureState) {
2020-01-04 19:31:52 +00:00
WidgetsBinding.instance.addPostFrameCallback((_) {
2020-09-25 15:32:44 +00:00
showPopUp<void>(
2020-01-04 19:31:52 +00:00
context: context,
builder: (_) {
return AlertWithOneAction(
alertTitle: S.current.new_wallet,
alertContent: state.error,
buttonText: S.of(context).ok,
2020-06-03 09:56:23 +00:00
buttonAction: () => Navigator.of(context).pop());
2020-01-04 19:31:52 +00:00
});
});
}
});
2020-06-20 07:10:00 +00:00
super.initState();
}
2020-01-04 19:31:52 +00:00
2020-06-20 07:10:00 +00:00
@override
Widget build(BuildContext context) {
final walletImage = getIt.get<SettingsStore>().isDarkTheme
2020-09-21 11:50:26 +00:00
? walletNameImage
: walletNameLightImage;
return Container(
padding: EdgeInsets.only(top: 24),
child: ScrollableWithBottomSection(
contentPadding: EdgeInsets.only(left: 24, right: 24, bottom: 24),
2020-06-03 09:56:23 +00:00
content:
Column(crossAxisAlignment: CrossAxisAlignment.center, children: [
Padding(
padding: EdgeInsets.only(left: 12, right: 12),
child: AspectRatio(
aspectRatio: aspectRatioImage,
child: FittedBox(child: walletImage, fit: BoxFit.fill)),
2020-06-03 09:56:23 +00:00
),
Padding(
padding: EdgeInsets.only(top: 24),
child: Form(
key: _formKey,
child: TextFormField(
2020-06-20 07:10:00 +00:00
onChanged: (value) => _walletNewVM.name = value,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.w600,
color:
Theme.of(context).primaryTextTheme.title.color),
decoration: InputDecoration(
hintStyle: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.w500,
2020-06-20 07:10:00 +00:00
color: Theme.of(context)
.accentTextTheme
.display3
2020-06-20 07:10:00 +00:00
.color),
hintText: S.of(context).wallet_name,
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context)
.accentTextTheme
.display3
.decorationColor,
2020-06-20 07:10:00 +00:00
width: 1.0)),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context)
.accentTextTheme
.display3
.decorationColor,
2020-06-20 07:10:00 +00:00
width: 1.0))),
validator: WalletNameValidator())),
),
2020-06-20 07:10:00 +00:00
if (_walletNewVM.hasLanguageSelector) ...[
Padding(
padding: EdgeInsets.only(top: 40),
child: Text(
S.of(context).seed_language_choose,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w500,
2020-06-20 07:10:00 +00:00
color: Theme.of(context).primaryTextTheme.title.color),
),
),
2020-06-20 07:10:00 +00:00
Padding(
padding: EdgeInsets.only(top: 24),
child: SeedLanguageSelector(
key: _languageSelectorKey,
initialSelected: defaultSeedLanguage),
)
]
]),
2020-06-03 09:56:23 +00:00
bottomSectionPadding:
EdgeInsets.only(left: 24, right: 24, bottom: 24),
bottomSection: Observer(
builder: (context) {
return LoadingPrimaryButton(
2020-06-20 07:10:00 +00:00
onPressed: _confirmForm,
text: S.of(context).seed_language_next,
color: Colors.green,
textColor: Colors.white,
2020-09-21 11:50:26 +00:00
isLoading: _walletNewVM.state is IsExecutingState,
2020-06-20 07:10:00 +00:00
isDisabled: _walletNewVM.name.isEmpty,
);
},
)),
);
2020-01-04 19:31:52 +00:00
}
2020-06-03 09:56:23 +00:00
2020-06-20 07:10:00 +00:00
void _confirmForm() {
2020-06-03 09:56:23 +00:00
if (!_formKey.currentState.validate()) {
return;
}
2020-06-20 07:10:00 +00:00
_walletNewVM.create(
options: _walletNewVM.hasLanguageSelector
? _languageSelectorKey.currentState.selected
: null);
2020-06-03 09:56:23 +00:00
}
2020-01-04 19:31:52 +00:00
}