我正在创建一个数据表并从 api 获取行中的值,并添加一个编辑图标按钮并单击编辑图标我想打开一个弹出对话框。但是当我点击编辑图标时,它会向我发送这个错误
错误:
[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3018 pos 18: '!navigator._debugLocked': is not true.
这是点击编辑图标后我的屏幕快照
这是我的代码
class MyAttendance extends StatefulWidget {
@override
_MyAttendanceState createState() => _MyAttendanceState();
}
class _MyAttendanceState extends State<MyAttendance> {
TextEditingController descriptionController;
TextEditingController timeinController;
TextEditingController timeoutController;
//this is my pop up dialogue box code
showAlertDialog(BuildContext context) {
// Create button
Widget okButton = FlatButton(
child: Text("Submit",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[900],fontSize: 20),),
onPressed: () {
Navigator.of(context).pop();
},
);
// Create AlertDialog
AlertDialog alert = AlertDialog(
insetPadding: EdgeInsets.symmetric(vertical: 160),
title: Text("Request to change time",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.blue[500])),
content: Column(children:<Widget> [
TextField(
decoration: InputDecoration(labelText: "Description"),
controller:descriptionController ,
),
TextField(
decoration: InputDecoration(labelText: "Time in"),
controller:timeinController ,
),
TextField(
decoration: InputDecoration(labelText: "Time out"),
controller:timeoutController ,
),
]),
actions: [
okButton,
],
);
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
//List<History> _historyList;
List<History> historyList=[];
String _selectedLeave;
int monthIndex;
int month;
var monthsList=<String>[
'January',
'Febuary',
'March',
'April',
'May',
'June',
'July',
'Augest',
'September',
'October',
'November',
'December'
];
String getdate="";
void _getDate() {
final String formattedDateTime =
DateFormat('MM').format(DateTime.now()).toString();
setState(() {
getdate = formattedDateTime;
print("date "+getdate);
});
}
_userDetails() async{
SharedPreferences myPrefs=await SharedPreferences.getInstance();
setState(() {
getname=myPrefs.getString('name');
});
}
void initState() {
_userDetails();
_getDate();
_getRecord();
}
Future<List<History>> _getRecord() async{
Dio dio=new Dio();
var data={
'username':getname,
'month':month,
'token':getaccesstoken
};
return dio
.post(localhostUrlAttendanceHistory,data: json.encode(data))
.then((onResponse) async {
Map<String, dynamic> map=onResponse.data;
print(map);
List<dynamic> data = map["data"];
// print(data[0]["Date"]);
print("sdf");
print(data);
print("time in");
//print(onResponse.data['data'][0]['TimeIn']);
//List<History> historyList = [];
for (var h in data) {
History history = History(
h["Date"],
h["TimeIn"],
h["TimeOut"],
);
historyList.add(history);
print(history);
}
return historyList;
})
.catchError((onerror){
print(onerror.toString());
});
}
Widget attendanceHistory(List<History>
historyList)=>
Center(
child:SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child:DataTable(
decoration: BoxDecoration(border: Border.all(color: Colors.blue[500], width: 2)),
headingRowColor: MaterialStateColor.resolveWith((states) => Colors.blue[500]),
headingTextStyle: const TextStyle(fontWeight: FontWeight.bold, color: Colors.white),
showBottomBorder: true,
headingRowHeight: 60,
horizontalMargin: 2,
columnSpacing: 15,
dataRowColor: MaterialStateColor.resolveWith((states) => Colors.blue[50]),
dividerThickness: 4,
columns: <DataColumn>[
DataColumn(label: Text("Date"),),
DataColumn(label: Text("Time in"),),
DataColumn(label: Text("Time out"),),
DataColumn(label: Text("Edit"),) //here i am creating edit column
// DataColumn(label: Text("Edit"),
],
rows:
historyList
?.map((element)=>DataRow(
cells: <DataCell>[
DataCell(Text(element?.date)),
DataCell(Text(element?.timeIn)),
DataCell(Text(element?.timeOut)),
//here i am creating edit icon and calling dialouge box
DataCell(IconButton(icon:Icon(Icons.edit,color: Colors.blue,),
onPressed: (){Navigator.push(context, MaterialPageRoute(builder: (context)=>showAlertDialog(context)));})),
// DataCell(Icon(Icons.edit)),
]) )?.toList()))));
TextEditingController fromDate=new TextEditingController();
TextEditingController toDate=new TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new MyAppBar(title: Text("My Attendance"),onpressed: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=>Profile()));
}),
body:Stack(children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(45, 80, 10, 0),
child:
DropdownButton<String>(
value: _selectedLeave==null?null:monthsList[monthIndex],
items:
monthsList
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value)
);
}).toList(),
hint:Text(
"Please choose a month",
),
onChanged: (String value) {
setState(() {
_selectedLeave=value;
monthIndex = monthsList.indexOf(value);
month=monthIndex+1;
print(month);
print(_selectedLeave);
});
},
),
),
Container(
padding: EdgeInsets.fromLTRB(15, 150, 0, 0),
child:SingleChildScrollView(
scrollDirection: Axis.vertical,
child: FutureBuilder(
future: _getRecord(),
builder: (BuildContext context, AsyncSnapshot<List<History>> snapshot) {
// Check if the data has been received.
if (snapshot.hasData) {
// Return the widget and provide the received data.
return attendanceHistory(snapshot.data);
}
//return Center(child: CircularProgressIndicator(),);
// print("data");
// print(snapshot.data);
return Center(
heightFactor: 10,
child:Text("Data is not avaliable",style: TextStyle(color: Colors.blue[900],fontWeight: FontWeight.bold,fontSize: 20),));
//return attendanceHistory(snapshot.data);
}
),
),)
]));
}
}
class History {
final String date;
final String timeIn;
final String timeOut;
History(this.date, this.timeIn, this.timeOut);
}
我也用过这个
onPressed: (){SchedulerBinding.instance.addPostFrameCallback((timeStamp)
{Navigator.push(context, MaterialPageRoute(builder: (context)=>showAlertDialog(context)));});})),
但它不起作用,同样的错误!
如果有人知道如何修复它,请帮忙,将不胜感激!