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

250 lines
9.3 KiB
Dart
Raw Normal View History

import 'package:cake_wallet/entities/generate_name.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/themes/theme_base.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';
import 'package:cake_wallet/core/wallet_name_validator.dart';
2020-06-20 07:10:00 +00:00
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
final walletNameImage = Image.asset('assets/images/wallet_name.png');
2021-02-03 17:29:48 +00:00
final walletNameLightImage =
Image.asset('assets/images/wallet_name_light.png');
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
Widget body(BuildContext context) => WalletNameForm(
_walletNewVM,
currentTheme.type == ThemeType.dark
? walletNameImage
: walletNameLightImage);
2020-01-04 19:31:52 +00:00
}
class WalletNameForm extends StatefulWidget {
WalletNameForm(this._walletNewVM, this.walletImage);
2020-06-20 07:10:00 +00:00
final WalletNewVM _walletNewVM;
final Image walletImage;
2020-06-20 07:10:00 +00:00
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;
2020-06-20 07:10:00 +00:00
final _formKey = GlobalKey<FormState>();
final _languageSelectorKey = GlobalKey<SeedLanguageSelectorState>();
ReactionDisposer _stateReaction;
final WalletNewVM _walletNewVM;
final TextEditingController _controller = TextEditingController();
@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.preSeed, arguments: _walletNewVM.type);
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) {
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: widget.walletImage, fit: BoxFit.fill)),
2020-06-03 09:56:23 +00:00
),
Padding(
padding: EdgeInsets.only(top: 24),
child: Form(
key: _formKey,
child: Stack(
alignment: Alignment.centerRight,
children: [
TextFormField(
onChanged: (value) => _walletNewVM.name = value,
controller: _controller,
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,
color: Theme.of(context)
.accentTextTheme
.display3
.color),
hintText: S.of(context).wallet_name,
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context)
.accentTextTheme
.display3
.decorationColor,
width: 1.0)),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context)
.accentTextTheme
.display3
.decorationColor,
width: 1.0),
),
2022-08-22 12:42:36 +00:00
suffixIcon: IconButton(
onPressed: () async {
final rName = await generateName();
FocusManager.instance.primaryFocus?.unfocus();
setState(() {
_controller.text = rName;
_walletNewVM.name = rName;
_controller.selection = TextSelection.fromPosition(
TextPosition(offset: _controller.text.length));
});
},
icon: Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6.0),
color: Theme.of(context).hintColor,
),
width: 34,
height: 34,
child: Image.asset(
'assets/images/refresh_icon.png',
color: Theme.of(context)
.primaryTextTheme
.display1
.decorationColor,
),
),
),
),
2022-08-22 12:42:36 +00:00
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.all(24),
bottomSection: Column(
children: [
Observer(
builder: (context) {
return LoadingPrimaryButton(
onPressed: _confirmForm,
text: S.of(context).seed_language_next,
color: Colors.green,
textColor: Colors.white,
isLoading: _walletNewVM.state is IsExecutingState,
isDisabled: _walletNewVM.name.isEmpty,
);
},
),
const SizedBox(height: 25),
GestureDetector(
onTap: () {
Navigator.of(context).pushNamed(Routes.privacySettings);
},
child: Text(S.of(context).advanced_privacy_settings),
),
],
)),
);
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;
}
if (_walletNewVM.nameExists(_walletNewVM.name)) {
showPopUp<void>(
context: context,
builder: (_) {
return AlertWithOneAction(
alertTitle: '',
alertContent: S.of(context).wallet_name_exists,
buttonText: S.of(context).ok,
buttonAction: () => Navigator.of(context).pop());
});
} else {
_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
}