从有状态类之外的小部件方法更新当前页面



我在有状态类之外有一个方法来显示对话框。我想在关闭对话框后刷新页面上的数据,它怎么可能是一个?如果我再次按下屏幕,它会表现得很奇怪(它会打开主页,然后转到当前页面,任何想法都可能帮助我解决问题。

AlertWidget(BuildContext context, String Fname, String Lname, String key) {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController _firstName = TextEditingController(text: Fname);
final TextEditingController _lastName = TextEditingController(text: Lname);
showDialog(
context: context,
builder: (context) {
var _height = MediaQuery.of(context).size.height;
var _width = MediaQuery.of(context).size.width;
return Center(
child: Material(
type: MaterialType.transparency,
child: Padding(
padding: const EdgeInsets.only(left: 15, right: 15),
child: Container(
color: Colors.white,
padding: EdgeInsets.all(15),
height: _height - _height * 11.5 / 16,
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Align(
alignment: (translator.currentLanguage == "en")
? Alignment.topLeft
: Alignment.topRight,
child: Text(
translator.translate('Name'),
style: TextStyle(
color: ProjectTheme.projectPrimaryColor,
fontSize: 25),
)),
SizedBox(
height: displayHeight(context) * 0.02,
),
Row(
children: [
Text("${translator.translate('FirstName')}:",
style: TextStyle(fontSize: 18)),
SizedBox(
height: _height * .8 / 16,
width: _width * 1.5 / 16,
),
Container(
height: _height * .5 / 16,
width: _width * 5.5 / 16,
child: Container(
child: Padding(
padding: const EdgeInsets.only(
left: 10, right: 10, top: 10),
child: TextFormField(
controller: _firstName,
validator: (value) {
if (value.trim().isEmpty)
return 'This field is required.';
return null;
},
decoration: InputDecoration(
border: InputBorder.none,
),
),
),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.4),
spreadRadius: 2,
blurRadius: 5,
offset: Offset(0, 1),
),
],
color: Colors.white,
),
),
),
],
),
Row(
children: [
Text("${translator.translate('LastName')}:",
style: TextStyle(fontSize: 18)),
SizedBox(
height: _height * 1 / 16,
width: _width * 1.5 / 16,
),
Container(
height: _height * .5 / 16,
width: _width * 5.5 / 16,
child: Container(
child: Padding(
padding: const EdgeInsets.only(
left: 10, right: 10, top: 10),
child: TextFormField(
controller: _lastName,
validator: (value) {
if (value.trim().isEmpty)
return 'This field is required.';
return null;
},
decoration: InputDecoration(
border: InputBorder.none,
),
),
),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.4),
spreadRadius: 2,
blurRadius: 5,
offset: Offset(0, 1),
),
],
color: Colors.white,
),
),
),
],
),
SizedBox(height: _height * .3 / 16),
Align(
alignment: Alignment.bottomRight,
child: Container(
height: 35,
width: 100,
decoration: BoxDecoration(
color: ProjectTheme.projectPrimaryColor,
borderRadius:
BorderRadius.all(Radius.circular(20.0)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 2,
blurRadius: 10,
offset: Offset(0, 3),
),
],
),
child: Center(
child: TextButton(
onPressed: () async {
bool isChanged = !(_firstName.text == Fname &&
_lastName == Lname);
if (_formKey.currentState.validate()) {
final FirebaseAuth _firebaseAuth =
FirebaseAuth.instance;
await FirebaseFirestore.instance
.collection('users')
.doc(_firebaseAuth.currentUser.uid)
.update((key == 'name')
? {
'firstName':
_firstName.text.trim(),
'lastName': _lastName.text.trim(),
'fullName':
_firstName.text.trim() +
' ' +
_lastName.text.trim(),
'userName':
_firstName.text.trim() +
' ' +
_lastName.text.trim(),
}
: {
key: _firstName.text.trim() +
' ' +
_lastName.text.trim()
});
FirebaseFirestore.instance
.collection('users')
.doc(_firebaseAuth.currentUser.uid)
.get()
.then((value) {
UserData newUser = UserData(
.......
);
Provider.of<AppData>(context, listen: false)
.updateUser(newUser);
// Navigator.of(context).pushReplacement(
//     MaterialPageRoute(
//         builder: (ctx) => profileScreen()));
});

}
},
child: Text(
translator.translate('Save'),
style: TextStyle(
fontSize: 16, color: Colors.white),
),
),
),
),
),
],
),
),
),
)),
);
},
);
}

很抱歉代码太长,我正在尽可能多地获取信息,如果你需要更多信息,请告诉我。

谢谢。

showDialog函数返回一个future,该future可用于查看对话框何时关闭。将此future返回到页面并将其await或调用页面上的then以了解何时完成。

import 'package:flutter/material.dart';
Future<void> AlertWidget(
BuildContext context,
String Fname,
String Lname,
String key,
) {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final TextEditingController _firstName = TextEditingController(text: Fname);
final TextEditingController _lastName = TextEditingController(text: Lname);
return showDialog(
context: context,
builder: (context) {
/*...*/
});
}
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
// this function opens the alert dialog & calls setState when the dialog is closed
void showDialogOnTap() {
// show dialog
AlertWidget(context, 'Fname', 'Lname', 'Pkey')
.then((_) { // then on closed
setState(() {}); // set page state
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(),
floatingActionButton: FloatingActionButton(
onPressed: showDialogOnTap,
),
);
}
}

最新更新