Use Provider.of instead of constructor params
This commit is contained in:
parent
f9d0c44c96
commit
f9814aa08a
|
@ -2,6 +2,7 @@ import 'dart:convert';
|
|||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_app/model.dart';
|
||||
import 'package:flutter_app/widgets/buttontextfield.dart';
|
||||
import 'package:flutter_app/widgets/cwtchlabel.dart';
|
||||
import 'package:flutter_app/widgets/passwordfield.dart';
|
||||
|
@ -13,10 +14,7 @@ import '../main.dart';
|
|||
import '../opaque.dart';
|
||||
|
||||
class AddEditProfileView extends StatefulWidget {
|
||||
const AddEditProfileView({Key key, this.profileOnion, this.profileDisplayName, this.profileImage="profiles/001-centaur.png"}) : super(key: key);
|
||||
final String profileOnion;
|
||||
final String profileDisplayName;
|
||||
final String profileImage;
|
||||
const AddEditProfileView({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_AddEditProfileViewState createState() => _AddEditProfileViewState();
|
||||
|
@ -28,22 +26,25 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
|
|||
final ctrlrNick = TextEditingController(text: "");
|
||||
final ctrlrPass = TextEditingController(text: "");
|
||||
final ctrlrPass2 = TextEditingController(text: "");
|
||||
TextEditingController ctrlrOnion;
|
||||
final ctrlrOnion = TextEditingController(text: "");
|
||||
bool usePassword;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
usePassword = true;
|
||||
ctrlrOnion = TextEditingController(text: widget.profileOnion);
|
||||
ctrlrNick.text = widget.profileDisplayName;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final nickname = Provider.of<ProfileInfoState>(context).nickname;
|
||||
if (nickname.isNotEmpty) {
|
||||
ctrlrNick.text = nickname;
|
||||
}
|
||||
ctrlrOnion.text = Provider.of<ProfileInfoState>(context).onion;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.profileOnion == "" ? AppLocalizations.of(context).addProfileTitle : AppLocalizations.of(context).editProfileTitle),
|
||||
title: Text(Provider.of<ProfileInfoState>(context).onion.isEmpty ? AppLocalizations.of(context).addProfileTitle : AppLocalizations.of(context).editProfileTitle),
|
||||
),
|
||||
body: _buildForm(),
|
||||
);
|
||||
|
@ -65,9 +66,10 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
|
|||
child: Container(
|
||||
margin: EdgeInsets.all(30),
|
||||
padding: EdgeInsets.all(20),
|
||||
child: SingleChildScrollView (child:Column(mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.stretch, children: [
|
||||
child: SingleChildScrollView(
|
||||
child: Column(mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.stretch, children: [
|
||||
Visibility(
|
||||
visible: widget.profileOnion.isNotEmpty,
|
||||
visible: Provider.of<ProfileInfoState>(context).onion.isNotEmpty,
|
||||
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
|
||||
SizedBox(
|
||||
width: 120,
|
||||
|
@ -81,7 +83,7 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
|
|||
width: 120,
|
||||
height: 120,
|
||||
child: Image(
|
||||
image: AssetImage("assets/" + widget.profileImage),
|
||||
image: AssetImage("assets/" + Provider.of<ProfileInfoState>(context).imagePath),
|
||||
width: 100,
|
||||
height: 100,
|
||||
))),
|
||||
|
@ -105,7 +107,7 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
|
|||
),
|
||||
]),
|
||||
Visibility(
|
||||
visible: widget.profileOnion != "",
|
||||
visible: Provider.of<ProfileInfoState>(context).onion.isNotEmpty,
|
||||
child: Column(mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: [
|
||||
SizedBox(
|
||||
height: 20,
|
||||
|
@ -123,7 +125,7 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
|
|||
])),
|
||||
// We only allow setting password types on profile creation
|
||||
Visibility(
|
||||
visible: widget.profileOnion == "",
|
||||
visible: Provider.of<ProfileInfoState>(context).onion.isEmpty,
|
||||
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
|
||||
Radio(
|
||||
value: false,
|
||||
|
@ -158,7 +160,7 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
|
|||
controller: ctrlrPass,
|
||||
validator: (value) {
|
||||
// Password field can be empty when just updating the profile, not on creation
|
||||
if (widget.profileOnion == "" && value.isEmpty && usePassword) {
|
||||
if (Provider.of<ProfileInfoState>(context, listen: false).onion.isEmpty && value.isEmpty && usePassword) {
|
||||
return AppLocalizations.of(context).passwordErrorEmpty;
|
||||
}
|
||||
if (value != ctrlrPass2.value.text) {
|
||||
|
@ -178,7 +180,7 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
|
|||
controller: ctrlrPass2,
|
||||
validator: (value) {
|
||||
// Password field can be empty when just updating the profile, not on creation
|
||||
if (widget.profileOnion == "" && value.isEmpty && usePassword) {
|
||||
if (Provider.of<ProfileInfoState>(context, listen: false).onion.isEmpty && value.isEmpty && usePassword) {
|
||||
return AppLocalizations.of(context).passwordErrorEmpty;
|
||||
}
|
||||
if (value != ctrlrPass.value.text) {
|
||||
|
@ -194,14 +196,14 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
|
|||
ElevatedButton(
|
||||
onPressed: _createPressed,
|
||||
style: ElevatedButton.styleFrom(primary: theme.current().defaultButtonColor()),
|
||||
child: Text(widget.profileOnion == "" ? AppLocalizations.of(context).addNewProfileBtn : AppLocalizations.of(context).saveProfileBtn),
|
||||
child: Text(Provider.of<ProfileInfoState>(context).onion.isEmpty ? AppLocalizations.of(context).addNewProfileBtn : AppLocalizations.of(context).saveProfileBtn),
|
||||
)
|
||||
]))));
|
||||
});
|
||||
}
|
||||
|
||||
void _copyOnion() {
|
||||
Clipboard.setData(new ClipboardData(text: widget.profileOnion));
|
||||
Clipboard.setData(new ClipboardData(text: Provider.of<ProfileInfoState>(context, listen: false).onion));
|
||||
// TODO Toast
|
||||
}
|
||||
|
||||
|
@ -210,7 +212,7 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
|
|||
// checking that display name is not empty, and an actual check that the passwords
|
||||
// match (and are provided if the user has requested an encrypted profile).
|
||||
if (_formKey.currentState.validate()) {
|
||||
if (widget.profileOnion.isEmpty) {
|
||||
if (Provider.of<ProfileInfoState>(context, listen: false).onion.isEmpty) {
|
||||
if (usePassword == true) {
|
||||
Provider.of<FlwtchState>(context, listen: false).cwtch.CreateProfile(ctrlrNick.value.text, ctrlrPass.value.text);
|
||||
Navigator.of(context).pop();
|
||||
|
@ -228,7 +230,7 @@ class _AddEditProfileViewState extends State<AddEditProfileView> {
|
|||
};
|
||||
final json = jsonEncode(event);
|
||||
|
||||
Provider.of<FlwtchState>(context, listen: false).cwtch.SendProfileEvent(widget.profileOnion, json);
|
||||
Provider.of<FlwtchState>(context, listen: false).cwtch.SendProfileEvent(Provider.of<ProfileInfoState>(context, listen: false).onion, json);
|
||||
Navigator.of(context).pop();
|
||||
} else {
|
||||
// TODO Update Password
|
||||
|
|
|
@ -64,9 +64,11 @@ class _ProfileMgrViewState extends State<ProfileMgrView> {
|
|||
Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (BuildContext context) {
|
||||
return Provider (
|
||||
create: (_) => Provider.of<FlwtchState>(context, listen: false),
|
||||
child: AddEditProfileView(profileOnion: onion),
|
||||
return MultiProvider (
|
||||
providers: [
|
||||
ChangeNotifierProvider<ProfileInfoState>(create: (_) => ProfileInfoState(onion: onion),),
|
||||
],
|
||||
builder: (context, widget) => AddEditProfileView(),
|
||||
);
|
||||
},
|
||||
)
|
||||
|
|
|
@ -73,9 +73,11 @@ class _ProfileRowState extends State<ProfileRow> {
|
|||
Navigator.of(context).push(
|
||||
MaterialPageRoute<void>(
|
||||
builder: (BuildContext context) {
|
||||
return Provider (
|
||||
create: (_) => Provider.of<FlwtchState>(context, listen: false),
|
||||
child: AddEditProfileView(profileOnion: onion, profileDisplayName: displayName, profileImage: profileImage,),
|
||||
return MultiProvider (
|
||||
providers: [
|
||||
ChangeNotifierProvider<ProfileInfoState>(create: (_) => ProfileInfoState(onion: onion, nickname:displayName, imagePath: profileImage),),
|
||||
],
|
||||
builder: (context, widget) => AddEditProfileView(),
|
||||
);
|
||||
},
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue