【发布时间】:2021-07-28 02:32:57
【问题描述】:
我有一个包含联系人列表的联系人页面和一个FloatingActionButton,它转到Add_Contact() 页面。
在AppBar 上有一个form 和一个FlatButton SAVE,单击它们会发布表单数据并返回到之前的联系人列表页面。
但是每当我点击保存按钮时,它只会停留在该页面上,不会返回到上一页。我还尝试添加SnackBar,然后添加FlushBar 以显示联系人已保存,但即使这样也不会显示在屏幕上。
contacts_page.dart:
//Rest of the code above
floatingActionButton: _bottomButtons(context),
));
}
Widget _bottomButtons(BuildContext context) {
return _tabController.index == 0
? FloatingActionButton(
shape: StadiumBorder(),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return AddContacts();
}));
},
backgroundColor: Colors.cyan,
child: Icon(
Icons.person_add,
color: Colors.white,
))
: FloatingActionButton(
shape: StadiumBorder(),
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (_) {
return AddCompany();
}));
},
backgroundColor: Colors.cyan,
child: Icon(
Icons.add,
color: Colors.white,
),
);
}
add_contacts.dart:
Future saveContact() async {
final String firstName = _firstName.text;
final String lastName = _lastName.text;
final String email = _email.text;
final AddContactModel contact =
await API_Manager().addContact(firstName, lastName, email);
Navigator.pop(context); //Not working
setState(() {
_contact = contact;
});
}
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Scaffold(
appBar: AppBar(
title: Text('Add Contact'),
actions: <Widget>[
FlatButton(
textColor: Colors.white,
onPressed: () async {
if (_formKey.currentState.validate()) {
await saveContact();
}
},
child: Text(
'SAVE',
style: TextStyle(
fontSize: 18,
color: Colors.white,
fontWeight: FontWeight.w600,
),
),
shape:
CircleBorder(side: BorderSide(color: Colors.transparent)),
)
],
),
body: SingleChildScrollView(
child: Container(
margin: EdgeInsets.all(5),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
onSaved: null,
controller: _ipCountryCode,
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: 'IP Country Code',
fillColor: Colors.white,
filled: true,
contentPadding: EdgeInsets.all(8)),
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Expanded(
child: DateTimeFormField(
decoration: InputDecoration(
labelText: 'Time First Seen',
fillColor: Colors.white,
filled: true,
contentPadding: EdgeInsets.all(8)),
onDateSelected: (DateTime value) {
setState(() {
timeFirstSeen = value;
});
},
),
),
],
),
//Rest of the Code Below
以下是两个屏幕的屏幕截图:
同样题外话,有没有更好的方法来发布具有大量字段的表单数据?目前我正在使用 HttpClient,我只能发布 3 个字段值 firstname、lastname 和 email。我尝试将表单中存在的其他字段添加到请求的正文和参数中,但它不会发布它们。我还尝试将整个 JSON 模型提供给请求的正文和函数的参数,但我不知道如何实际调用请求函数并将 TextFormField 控制器分配给它的值以发布数据。如果你也能帮我解决这个问题,那就太好了。
编辑:
API_Manager():
Future<AddContactModel> addContact(
String firstName, String lastName, String email) async {
var client = http.Client();
String addContactUrl =
"https://example.com/ma/api/contacts/new";
String basicAuth = 'Basic examplebasicauthkey';
var response = await client.post(addContactUrl, headers: <String, String>{
'authorization': basicAuth,
"Accept": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
}, body: {
"firstname": firstName,
"lastname": lastName,
"email": email,
});
// print(response.statusCode);
//developer.log(response.body);
if (response.statusCode == 201) {
final String responseString = response.body;
return addContactModelFromJson(responseString);
} else {
return null;
}
}
【问题讨论】:
-
你确定
_formKey.currentState.validate()是真的吗? -
我没有在您的表单字段中看到任何验证器功能。我编写了一个测试应用程序,如果没有验证器,
_formKey.currentState.validate()将返回 true。您的API_Manager()中可能有错误 -
@LuisA.Chaglla 是的,因为数据正在发布到服务器。
-
您是否遇到任何错误?如果没有,也许对
API_Manager().addContact的调用永远不会解决
标签: flutter dart post flutter-navigation scaffold