Я добавил это, чтобы показать пример, измените его для своей базы данных!
Главный экран
class MainScreen extends StatefulWidget {
@override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
List<Map> list = [];
@override
void initState() {
super.initState();
list.add({'name': 'Raj', 'phone': '1234567890'});
list.add(
{'name': 'Naveen', 'phone': '1122334455'},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text('Main Screen', style: TextStyle(color: Colors.white)),
actions: [
IconButton(
icon: Icon(Icons.add_circle, color: Colors.white),
onPressed: () {
addNewItem();
})
]),
body: Container(
padding:EdgeInsets.all(15),
color: Colors.white,
child: Center(
child: ListView.builder(
itemCount: list.length,
itemBuilder: (con, ind) {
return ListTile(
title: Text(list[ind]['name'],
style: TextStyle(color: Colors.black)),
subtitle: Text(list[ind]['phone'],
style: TextStyle(color: Colors.black)));
}),
)),
);
}
void addNewItem() async {
Map newItem = await Navigator.push(
context, MaterialPageRoute(builder: (cc) => NewScreen()));
if (newItem != null) {
setState(() {
list.add(newItem);
});
}
}
}
Новый экран
class NewScreen extends StatefulWidget {
@override
_NewScreenState createState() => _NewScreenState();
}
class _NewScreenState extends State<NewScreen> {
//Declare your new item here....
Map newItem;
//I m using strigns, but use TextEditingControllers here...
String name = '', phone = '';
@override
void initState() {
super.initState();
//database initialisation...
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: onBackPressed,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text('New Screen', style: TextStyle(color: Colors.white)),),
body: Container(
padding:EdgeInsets.all(15),
color:Colors.white,
child: Center(
child: Column(
children: <Widget>[
TextField(
decoration: InputDecoration(labelText: 'Name',
labelStyle: TextStyle(color: Colors.black)),
onChanged: (v) {
setState(() {
name = v;
});
},
style: TextStyle(color: Colors.black)),
TextField(
decoration: InputDecoration(labelText: 'Phone',
labelStyle: TextStyle(color: Colors.black)),
onChanged: (v) {
setState(() {
phone = v;
});
},
style: TextStyle(color: Colors.black)),
RaisedButton(
child: Text('Add'),
onPressed: () {
if (name.isNotEmpty && phone.isNotEmpty) {
newItem = Map();
newItem['name'] = name;
newItem['phone'] = phone;
//You may close new screen if you want !
//But call sendNewItemBack();
}
})
],
))),
),
);
}
Future<bool> onBackPressed() async {
sendNewItemBack();
}
sendNewItemBack() {
//add new item to database
Navigator.of(context).pop(newItem);
}
}
После добавления
![after pic](https://i.stack.imgur.com/zsYNQ.jpg)