如何在成功数据后显示对话框



我想显示对话框">添加成功";在我的申请中add/ update data之后。我试着使用显示对话框,但它不起作用。所以我需要你的帮助

添加主管类:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:finalyearproject/model/NewUser.dart';
import 'package:finalyearproject/service/database.dart';


class AddSupervisor extends StatefulWidget {

@override
_AddSupervisorState createState() => _AddSupervisorState();
}
class _AddSupervisorState extends State<AddSupervisor> {
//text field
String name = ' ';
String email = ' ';
String uniqueID = ' ';
String phone = ' ';
String error;
String id = Firestore.instance.collection('Supervisor').document().documentID;
final GlobalKey<FormState> _formKey = GlobalKey();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Supervisor'),
backgroundColor: Colors.redAccent,
),
body: Form(
key: _formKey,
child: SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
SizedBox(height: 25.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Name',
prefixIcon: Icon(Icons.person),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.text,
validator: (value) => value.isEmpty ? 'Name cannot be empty!': null,
onChanged: (value) {
setState(() => name = value);
},
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Email',
prefixIcon: Icon(Icons.email),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.emailAddress,
validator: (value) => value.isEmpty ? 'Email cannot be empty!': null,
onChanged: (value) {
setState(() => email = value);
},
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Number Phone',
prefixIcon: Icon(Icons.phone),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.number,
validator: (value) => value.isEmpty ? 'Number Phone cannot be empty!': null,
onChanged: (value) {
setState(() => phone = value);
},
),
SizedBox(height: 10.0),
TextFormField(
decoration: InputDecoration(
hintText: 'Unique ID ',
prefixIcon: Icon(Icons.perm_contact_calendar),
border: OutlineInputBorder(borderRadius: BorderRadius.circular(5))),
keyboardType: TextInputType.number,
validator: (value) => value.isEmpty ? 'Ic number cannot be empty!': null,
onChanged: (value) {
setState(() => uniqueID = value);
},
),
const SizedBox(height: 20.0),
RaisedButton(
color: Colors.redAccent,
textColor: Colors.black,
child: Text("Save"),
onPressed: () async {
if(_formKey.currentState.validate()){
DatabaseService().addSupervisor(NewUser(name: name, email: email, uniqueID: uniqueID, nophone: phone, id: id));
Navigator.pop(context); 
_formKey.currentState.save();
} else {
print("Must be complete all!");
}
}
),
],
),
),
),
);
}
}

我想把显示对话框放在行导航器弹出窗口中,但我仍然不知道如何把显示对话框放到那里。有人知道吗?真的需要你的帮助。

Future<bool> alertDialog( BuildContext context) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Done'),
content: Text('Add Success'),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
Navigator.pop(context);
},
),
],
);
});
}

每当你想调用它时,请使用以下代码:

await alertDialog(
context,
);

或者更好的是,你可以使用FLutterToast包

import 'package:fluttertoast/fluttertoast.dart';
Future<bool> toast(String message) {
Fluttertoast.cancel();
return Fluttertoast.showToast(
msg: message,
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
timeInSecForIos: 4,
backgroundColor: Colors.redAccent,
textColor: Colors.white,
fontSize: 15.0);
}

以及每当你想为成功的连接调用函数显示toast时

toast("Success Data");

注意

Fluttertoast.cancel();

用于在您尝试调用的吐司之前消除任何剩余的吐司。

相关内容

  • 没有找到相关文章

最新更新