在我的应用中,我有一个UserProfile,其中包含一个私钥。首次启动该应用程序时,它会检查pk是否存在,如果不存在,则显示输入pk的表格。所有这些都很好。现在,我在将输入的值存储到UserProfile中时遇到一些问题。这是我的表格:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:voota/widgets/AppBar.dart';
import 'package:voota/widgets/Button.dart';
import 'package:voota/utils/Hex2Color.dart';
import 'package:voota/utils/UserProfile_dbHelper.dart';
class SetPrivateKey extends StatefulWidget {
@override
_PrivateKey createState() => _PrivateKey();
}
class _PrivateKey extends State<SetPrivateKey> {
final String title = 'Set Private Key';
final bgcolor = HexToColor('#ffffff');
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: BaseAppBar(title: title,appBar: AppBar()),backgroundColor: bgcolor,body: Center(
child: PrivateKeyForm(),),);
}
}
class PrivateKeyForm extends StatefulWidget {
@override
_PrivateKeyForm createState() => _PrivateKeyForm();
}
class _PrivateKeyForm extends State<PrivateKeyForm> {
final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
final TextEditingController _controller = new TextEditingController();
var _userprofile = UserProfileDatabaseHelper().getUserProfile();
@override
Widget build(BuildContext context) {
final int maxLength = 5;
return Form(
key: _formKey,child: new Container(
padding: const EdgeInsets.all(30.0),color: Colors.white,child: new Container(
child: new Center(
child: new Column(children: [
new Padding(padding: EdgeInsets.only(top: 20.0)),new Text(
'Enter or Scan your Private Key',style:
new TextStyle(color: HexToColor("#F2A03D"),fontSize: 14.0),new Padding(padding: EdgeInsets.only(top: 30.0)),new TextFormField(
maxLength: maxLength,decoration: new InputDecoration(
labelText: "Private Key",fillColor: Colors.white,border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(15.0),borderSide:
new BorderSide(color: Colors.orange,width: 2.0),validator: (value) {
if (value.isEmpty || value.length < maxLength) {
return "Enter a valid private key";
} else {
return null;
}
},onSaved: (value) async {
print('onSaved');
},style: new TextStyle(
fontFamily: "Poppins",new Container(
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,children: <Widget>[
new VootaButton(
text: 'Scan',textColor: Colors.white,buttonColor: Colors.green,onpressed: () {}),new RaisedButton.icon(
label: new Text("Save",style: new TextStyle(
color: HexToColor("#FFFFFF"),fontSize: 14.0)),icon:
new Icon(Icons.save,color: HexToColor("#FFFFFF")),color: HexToColor('#508bbb'),elevation: 5.0,onpressed: _submitForm),],])),)),);
}
void _submitForm() {
final FormState formState = _formKey.currentState;
if (!formState.validate()) {
Scaffold.of(context).showsnackBar(snackBar(
content: Text('Form is not valid,please check !!'),backgroundColor: Colors.redaccent,));
} else {
// hide keyboard
FocusScope.of(context).requestFocus(Focusnode());
// display a snackbar.
Scaffold.of(context).showsnackBar(snackBar(
content: Text('Processing and validating Private Key...'),backgroundColor: Colors.orange,));
formState.save();
}
}
}
onSaved:执行得很好,正是在这里我需要您的帮助。我只是想更新pk(如果UserProfile已经存在),否则,请创建一个新的。欢迎任何帮助,建议或链接。谢谢。