Flutter 颤振表单组未看到提交功能
因此,我遇到了Form Bloc无法访问EditaccountFormBloc类中的onSubmitting()函数的问题。。我使用了打印语句,它正常到达验证函数,但没有到达提交函数。。下面是课堂:Flutter 颤振表单组未看到提交功能,flutter,dart,bloc,Flutter,Dart,Bloc,因此,我遇到了Form Bloc无法访问EditaccountFormBloc类中的onSubmitting()函数的问题。。我使用了打印语句,它正常到达验证函数,但没有到达提交函数。。下面是课堂: class CommonThings { static Size size; } class EditaccountFormBloc extends FormBloc<String, String> { final _usernameFieldBloc = Text
class CommonThings {
static Size size;
}
class EditaccountFormBloc extends FormBloc<String, String> {
final _usernameFieldBloc =
TextFieldBloc(asyncValidatorDebounceTime: Duration(milliseconds: 300));
static final _firstnameFieldBloc =
TextFieldBloc(asyncValidatorDebounceTime: Duration(milliseconds: 300));
static final _lastnameFieldBloc =
TextFieldBloc(asyncValidatorDebounceTime: Duration(milliseconds: 300));
final _emailFieldBloc =
TextFieldBloc(asyncValidatorDebounceTime: Duration(milliseconds: 300));
final _univFieldBloc =
TextFieldBloc(asyncValidatorDebounceTime: Duration(milliseconds: 300));
final _majorFieldBloc =
TextFieldBloc(asyncValidatorDebounceTime: Duration(milliseconds: 300));
final _passwordFieldBloc =
TextFieldBloc(asyncValidatorDebounceTime: Duration(milliseconds: 300));
@override
List<FieldBloc> get fieldBlocs => [
_usernameFieldBloc,
_firstnameFieldBloc,
_lastnameFieldBloc,
_emailFieldBloc,
_univFieldBloc,
_majorFieldBloc,
_passwordFieldBloc
];
EditaccountFormBloc() {
//retrieve info
String currentUsername = Auth.getCurrentUsername();
String currentEmail = Auth.getCurrentUserEmail();
//validators
_usernameFieldBloc.addAsyncValidators([_validateusername]);
_emailFieldBloc.addAsyncValidators([_validateemail]);
_passwordFieldBloc.addAsyncValidators([_validatepassword]);
//initial values
_usernameFieldBloc.updateInitialValue(currentUsername);
_emailFieldBloc.updateInitialValue(currentEmail);
}
void updatevalues(String first, String last) async {
_firstnameFieldBloc.updateInitialValue(first);
_lastnameFieldBloc.updateInitialValue(last);
}
Future<String> _validateusername(String tusername) async {
print("inside validation");
// validate if username exists
await Future<void>.delayed(Duration(milliseconds: 200));
if (!await Auth.checkUsernameAvailability(tusername)) {
if (tusername != Auth.getCurrentUsername())
return "Username already taken";
}
if (tusername.length < 3) {
return "Too short username";
}
return null;
}
Future<String> _validateemail(String temail) async {
// validate if email exists
String isValid = Auth.validateEmail(temail);
if (isValid != null) {
return isValid;
}
await Future<void>.delayed(Duration(milliseconds: 200));
if (!await Auth.checkemailAvailability(temail)) {
if (temail != Auth.getCurrentUserEmail()) return "Email already exists";
}
return null;
}
Future<String> _validatepassword(String tpass) async {
// validate if username exists
return Auth.validatePassword(tpass);
}
StreamSubscription<TextFieldBlocState> _textFieldBlocSubscription;
@override
void dispose() {
_usernameFieldBloc.dispose();
_firstnameFieldBloc.dispose();
_lastnameFieldBloc.dispose();
_emailFieldBloc.dispose();
_passwordFieldBloc.dispose();
_textFieldBlocSubscription.cancel();
super.dispose();
}
@override
Stream<FormBlocState<String, String>> onSubmitting() async* {
print("inside submission");
try {
String uID = Auth.getCurrentUserID();
await FirebaseFirestore.instance.collection('User').doc(uID).update({
'email': _emailFieldBloc.value,
'userName': _usernameFieldBloc.value,
'firstName': _firstnameFieldBloc.value,
'lastName': _lastnameFieldBloc.value,
});
await Auth.auth.currentUser.updateProfile(
displayName: _firstnameFieldBloc.value,
);
yield currentState.toSuccess();
} catch (e) {
yield currentState.toFailure(e);
}
}
}
class普通事物{
静态尺寸;
}
类EditaccountFormBloc扩展了FormBloc{
最终用户名称字段组=
TextFieldBloc(asyncValidatorDebounceTime:Duration(毫秒:300));
静态最终_firstnameFieldBloc=
TextFieldBloc(asyncValidatorDebounceTime:Duration(毫秒:300));
静态最终\u lastnameFieldBloc=
TextFieldBloc(asyncValidatorDebounceTime:Duration(毫秒:300));
最终(欧盟)=
TextFieldBloc(asyncValidatorDebounceTime:Duration(毫秒:300));
最终_univFieldBloc=
TextFieldBloc(asyncValidatorDebounceTime:Duration(毫秒:300));
最终(主要领域集团)=
TextFieldBloc(asyncValidatorDebounceTime:Duration(毫秒:300));
最终密码字段组=
TextFieldBloc(asyncValidatorDebounceTime:Duration(毫秒:300));
@凌驾
List get fieldBlocs=>[
_usernameFieldBloc,
_firstnameFieldBloc,
_lastnameFieldBloc,
_国际集团,
_univFieldBloc,
_马约菲尔德集团,
_passwordFieldBloc
];
EditaccountFormBloc(){
//检索信息
字符串currentUsername=Auth.getCurrentUsername();
字符串currentEmail=Auth.getCurrentUserEmail();
//验证器
_usernameFieldBloc.addAsyncValidators([\u validateusername]);
_emailFieldBloc.addAsyncValidators([\u validateemail]);
_passwordFieldBloc.addAsyncValidators([\u validatepassword]);
//初始值
_usernameFieldBloc.updateInitialValue(当前用户名);
_emailFieldBloc.updateInitialValue(当前电子邮件);
}
void updateValue(字符串优先,字符串最后)异步{
_firstnameFieldBloc.updateInitialValue(第一个);
_lastnameFieldBloc.updateInitialValue(最后一个);
}
Future\u validateusername(字符串tusername)异步{
打印(“内部验证”);
//验证用户名是否存在
等待未来。延迟(持续时间(毫秒:200));
如果(!wait Auth.checkUserName可用性(tusername)){
if(tusername!=Auth.getCurrentUsername())
返回“用户名已使用”;
}
如果(t长度<3){
返回“用户名太短”;
}
返回null;
}
Future\u validateemail(字符串temail)异步{
//验证电子邮件是否存在
String isValid=Auth.validateEmail(temail);
if(isValid!=null){
返回有效;
}
等待未来。延迟(持续时间(毫秒:200));
如果(!等待验证检查电子邮件可用性(temail)){
如果(temail!=Auth.getCurrentUserEmail())返回“电子邮件已存在”;
}
返回null;
}
未来验证密码(字符串tpass)异步{
//验证用户名是否存在
返回验证密码(tpass);
}
StreamSubscription _textFieldBlocSubscription;
@凌驾
无效处置(){
_usernameFieldBloc.dispose();
_dispose();
_dispose();
_emailFieldBloc.dispose();
_passwordFieldBloc.dispose();
_textFieldBlocSubscription.cancel();
super.dispose();
}
@凌驾
流提交()异步*{
打印(“内部提交”);
试一试{
字符串uID=Auth.getCurrentUserID();
等待FirebaseFirestore.instance.collection('User').doc(uID).update({
“email”:u emailFieldBloc.value,
'userName':_usernameFieldBloc.value,
“firstName”:_firstnameFieldBloc.value,
“lastName”:\u lastnameFieldBloc.value,
});
等待Auth.Auth.currentUser.updateProfile(
displayName:_firstnameFieldBloc.value,
);
屈服currentState.toSuccess();
}捕获(e){
屈服电流状态toFailure(e);
}
}
}
下面是表单小部件:
class EditAccountPage extends StatelessWidget {
String displayName;
String email;
String password;
bool isLoading = false;
@override
Widget build(BuildContext context) {
return buildBody(context);
}
Widget buildBody(BuildContext context) {
CollectionReference users = FirebaseFirestore.instance.collection('User');
String firstname;
String lastname;
return new WillPopScope(
onWillPop: () async => false,
child: BlocProvider<EditaccountFormBloc>(
builder: (context) => EditaccountFormBloc(),
child: FutureBuilder<DocumentSnapshot>(
future: users.doc(Auth.getCurrentUserID()).get(),
builder: (BuildContext context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
final _EditaccountFormBloc =
BlocProvider.of<EditaccountFormBloc>(context);
if (snapshot.hasData) {
Map<String, dynamic> data = snapshot.data.data();
firstname = data['firstName'];
lastname = data['lastName'];
_EditaccountFormBloc.updatevalues(firstname, lastname);
}
CommonThings.size = MediaQuery.of(context).size;
return Scaffold(
body: FormBlocListener<EditaccountFormBloc, String, String>(
onSubmitting: (context, state) {
// Show the progress dialog
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => WillPopScope(
onWillPop: () async => false,
child: Center(
child: Card(
child: Container(
width: 80,
height: 80,
padding: EdgeInsets.all(12.0),
child: CircularProgressIndicator(),
),
),
),
),
);
},
onSuccess: (context, state) {
Navigator.of(context).pop();
Fluttertoast.showToast(
msg: "Updates saved",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.blueAccent,
textColor: Colors.white,
fontSize: 16.0);
// Navigate to success screen
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => MoreInfoPage()));
},
onFailure: (context, state) {
// Hide the progress dialog
Navigator.of(context).pop();
Fluttertoast.showToast(
msg: "An error accured",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
backgroundColor: Colors.blueAccent,
textColor: Colors.white,
fontSize: 16.0);
// Show snackbar with the error
},
child: isLoading
? Center(
child: CircularProgressIndicator(),
)
: Scaffold(
appBar: AppBar(
leading: Container(),
centerTitle: true,
backgroundColor: Color(0xff2A4793),
title: Text(
"Edit Account",
textAlign: TextAlign.center,
),
),
body: Center(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
width: 250.0,
child: TextFieldBlocBuilder(
textFieldBloc:
_EditaccountFormBloc._usernameFieldBloc,
suffixButton: SuffixButton
.circularIndicatorWhenIsAsyncValidating,
//
decoration: new InputDecoration(
border: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.red)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.indigo, width: 2.0),
),
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.indigo),
),
disabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.red)),
errorBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.red)),
contentPadding: EdgeInsets.only(
left: 15,
bottom: 11,
top: 11,
right: -20),
hintText: "Username",
),
),
),
SizedBox(height: CommonThings.size.height * 0.01),
Container(
width: 250.0,
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Container(
width: 125.0,
child: TextFieldBlocBuilder(
textFieldBloc: EditaccountFormBloc
._firstnameFieldBloc,
suffixButton: SuffixButton
.circularIndicatorWhenIsAsyncValidating,
decoration: new InputDecoration(
labelText: 'First name',
border: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.red)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.indigo,
width: 2.0),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.indigo),
),
disabledBorder:
UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.red)),
errorBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.red)),
contentPadding: EdgeInsets.only(
left: 15,
bottom: 11,
top: 11,
right: -30),
hintText: "First name",
),
),
),
Container(
width: 125.0,
child: TextFieldBlocBuilder(
textFieldBloc: EditaccountFormBloc
._lastnameFieldBloc,
suffixButton: SuffixButton
.circularIndicatorWhenIsAsyncValidating,
maxLines: 1,
decoration: new InputDecoration(
labelText: 'Last name',
border: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.red)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.indigo,
width: 2.0),
),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.indigo),
),
disabledBorder:
UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.red)),
errorBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.red)),
contentPadding: EdgeInsets.only(
left: 15,
bottom: 11,
top: 11,
right: -30),
isDense: true,
hintText: "Last name",
),
),
),
],
)),
SizedBox(height: CommonThings.size.height * 0.01),
SizedBox(height: CommonThings.size.height * 0.01),
Container(
width: 250.0,
child: TextFieldBlocBuilder(
textFieldBloc:
_EditaccountFormBloc._emailFieldBloc,
suffixButton: SuffixButton
.circularIndicatorWhenIsAsyncValidating,
//
decoration: new InputDecoration(
border: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.red)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.indigo, width: 2.0),
),
enabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.indigo),
),
disabledBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.red)),
errorBorder: UnderlineInputBorder(
borderSide:
BorderSide(color: Colors.red)),
contentPadding: EdgeInsets.only(
left: 15,
bottom: 11,
top: 11,
right: -20),
hintText: "Email",
),
),
),
RoundedButton(
text: "Save",
color: Colors.indigo,
textColor: Colors.white,
press: _EditaccountFormBloc.submit,
),
SizedBox(
height: CommonThings.size.height * 0.003),
RoundedButton(
text: "Cancel",
color: Colors.blue[100],
textColor: Colors.black,
press: () {
Navigator.of(context).pop();
},
)
],
),
)),
),
));
},
),
));
}
}
class EditAccountPage扩展了无状态小部件{
字符串显示名;
字符串电子邮件;
字符串密码;
bool isLoading=false;
@凌驾
小部件构建(构建上下文){
返回buildBody(上下文);
}
小部件构建体(构建上下文){
CollectionReference users=FirebaseFirestore.instance.collection('User');
字符串名;
字符串lastname;
返回新的Willposcope(
onWillPop:()async=>false,
孩子:BlocProvider(
生成器:(上下文)=>EditaccountFormBloc(),
孩子:未来建设者(
future:users.doc(Auth.getCurrentUserID()).get(),
生成器:(BuildContext上下文,
异步快照(快照){
最终(编辑组)=
(上下文)的BlocProvider;
if(snapshot.hasData){
映射数据=snapshot.data.data();
firstname=数据['firstname'];
lastname=data['lastname'];
_EditaccountFormBloc.UpdateValue(firstname,lastname);
}
CommonThings.size=MediaQuery.of(context).size;
返回脚手架(
正文:FormBlocListener(
提交:(上下文、状态){
//显示进度对话框
显示对话框(
上下文:上下文,
禁止:错误,
建筑商:()=>Willposcope(
onWillPop:()async=>false,
儿童:中心(
孩子:卡片(
子:容器(
宽度:80,
身高:80,
填充:所有边缘设置(12.0),
子对象:CircularProgressIndicator(),
),
),
),
),
);
},
onSuccess:(上下文)