如何通过按下按钮从另一个 class 更新地图
How to update Map from another class via button press
我在屏幕上有一些基于地图内容动态呈现的小部件。我已经通过文本字段更新了地图的内容,并且在我决定将此文本字段移动到单独的 class / 屏幕之前它一直运行良好。如何从这个单独的屏幕正确调用 setState 并在 return?
时反映我的更新
附带说明一下,我花了很多时间试图研究这个问题,但除了可能需要使用回调函数之外,我似乎无法解决任何问题?
NoteSection 是从提醒地图动态呈现的
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
final Map<String, String> reminders = {};
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddNoteScreen(),
),
);
},
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
NoteSection(),
],
),
);
}
}
我正在更新地图的屏幕代码如下
class AddNoteScreen extends StatefulWidget {
@override
_AddNoteScreenState createState() => _AddNoteScreenState();
}
final titleController = TextEditingController();
final bodyController = TextEditingController();
String noteBodyText = bodyController.text;
String noteTitleText = titleController.text;
class _AddNoteScreenState extends State<AddNoteScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
titleTextField,
bodyTextField,
Container(
padding: EdgeInsets.only(
right: 10,
bottom: 10,
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.amber,
),
child: Text(
'Add Note',
style: TextStyle(color: Colors.white),
),
onPressed: () {
setState(() {
noteTitleText = titleController.text;
noteBodyText = bodyController.text;
reminders[noteTitleText] = noteBodyText;
titleController.clear();
bodyController.clear();
Navigator.pop(context);
});
},
),
),
],
),
);
}
}
您应该通过构造函数将 noteBodyText 传递给 myhomepage statefull 小部件,这样您的 myHomePage 应该看起来像这样
class MyHomePage extends StatefulWidget {
String noteBodyText;
MyHomePage({this.noteBodyText});
@override
_MyHomePageState createState() => _MyHomePageState();
}
final Map<String, String> reminders = {};
class _MyHomePageState extends State<MyHomePage> {
void initState(){
reminders[noteTitleText] = widget.noteBodyText;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddNoteScreen(),
),
);
},
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
NoteSection(),
],
),
);
}
}
addnotescreen 应如下所示
class AddNoteScreen extends StatefulWidget {
@override
_AddNoteScreenState createState() => _AddNoteScreenState();
}
final titleController = TextEditingController();
final bodyController = TextEditingController();
String noteBodyText = bodyController.text;
String noteTitleText = titleController.text;
class _AddNoteScreenState extends State<AddNoteScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
titleTextField,
bodyTextField,
Container(
padding: EdgeInsets.only(
right: 10,
bottom: 10,
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.amber,
),
child: Text(
'Add Note',
style: TextStyle(color: Colors.white),
),
onPressed: () {
noteTitleText = titleController.text;
noteBodyText = bodyController.text;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyHomePage(noteBodyText:noteBodyText),
),
);
},
),
),
],
),
);
}
}
这有效
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
final Map<String, String> reminders = {};
List<String> _information = [];
class _MyHomePageState extends State<MyHomePage> {
void updateInformation(List<String> information) {
setState(() => reminders[information[0]] = information[1]);
}
void moveToSecondPage() async {
List<String> information = await Navigator.push(
context,
MaterialPageRoute(
fullscreenDialog: true, builder: (context) => AddNoteScreen()),
);
updateInformation(information);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
moveToSecondPage();
},
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
NoteSection(),
],
),
);
}
}
添加笔记屏幕
class AddNoteScreen extends StatefulWidget {
@override
_AddNoteScreenState createState() => _AddNoteScreenState();
}
final titleController = TextEditingController();
final bodyController = TextEditingController();
String noteBodyText = bodyController.text;
String noteTitleText = titleController.text;
class _AddNoteScreenState extends State<AddNoteScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
titleTextField,
bodyTextField,
Container(
padding: EdgeInsets.only(
right: 10,
bottom: 10,
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.amber,
),
child: Text(
'Add Note',
style: TextStyle(color: Colors.white),
),
onPressed: () {
noteTitleText = titleController.text;
noteBodyText = bodyController.text;
Navigator.pop(context,[noteTitleText,noteBodyText]);
},
),
),
],
),
);
}
}
我在屏幕上有一些基于地图内容动态呈现的小部件。我已经通过文本字段更新了地图的内容,并且在我决定将此文本字段移动到单独的 class / 屏幕之前它一直运行良好。如何从这个单独的屏幕正确调用 setState 并在 return?
时反映我的更新附带说明一下,我花了很多时间试图研究这个问题,但除了可能需要使用回调函数之外,我似乎无法解决任何问题?
NoteSection 是从提醒地图动态呈现的
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
final Map<String, String> reminders = {};
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddNoteScreen(),
),
);
},
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
NoteSection(),
],
),
);
}
}
我正在更新地图的屏幕代码如下
class AddNoteScreen extends StatefulWidget {
@override
_AddNoteScreenState createState() => _AddNoteScreenState();
}
final titleController = TextEditingController();
final bodyController = TextEditingController();
String noteBodyText = bodyController.text;
String noteTitleText = titleController.text;
class _AddNoteScreenState extends State<AddNoteScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
titleTextField,
bodyTextField,
Container(
padding: EdgeInsets.only(
right: 10,
bottom: 10,
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.amber,
),
child: Text(
'Add Note',
style: TextStyle(color: Colors.white),
),
onPressed: () {
setState(() {
noteTitleText = titleController.text;
noteBodyText = bodyController.text;
reminders[noteTitleText] = noteBodyText;
titleController.clear();
bodyController.clear();
Navigator.pop(context);
});
},
),
),
],
),
);
}
}
您应该通过构造函数将 noteBodyText 传递给 myhomepage statefull 小部件,这样您的 myHomePage 应该看起来像这样
class MyHomePage extends StatefulWidget {
String noteBodyText;
MyHomePage({this.noteBodyText});
@override
_MyHomePageState createState() => _MyHomePageState();
}
final Map<String, String> reminders = {};
class _MyHomePageState extends State<MyHomePage> {
void initState(){
reminders[noteTitleText] = widget.noteBodyText;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AddNoteScreen(),
),
);
},
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
NoteSection(),
],
),
);
}
}
addnotescreen 应如下所示
class AddNoteScreen extends StatefulWidget {
@override
_AddNoteScreenState createState() => _AddNoteScreenState();
}
final titleController = TextEditingController();
final bodyController = TextEditingController();
String noteBodyText = bodyController.text;
String noteTitleText = titleController.text;
class _AddNoteScreenState extends State<AddNoteScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
titleTextField,
bodyTextField,
Container(
padding: EdgeInsets.only(
right: 10,
bottom: 10,
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.amber,
),
child: Text(
'Add Note',
style: TextStyle(color: Colors.white),
),
onPressed: () {
noteTitleText = titleController.text;
noteBodyText = bodyController.text;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MyHomePage(noteBodyText:noteBodyText),
),
);
},
),
),
],
),
);
}
}
这有效
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
final Map<String, String> reminders = {};
List<String> _information = [];
class _MyHomePageState extends State<MyHomePage> {
void updateInformation(List<String> information) {
setState(() => reminders[information[0]] = information[1]);
}
void moveToSecondPage() async {
List<String> information = await Navigator.push(
context,
MaterialPageRoute(
fullscreenDialog: true, builder: (context) => AddNoteScreen()),
);
updateInformation(information);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
moveToSecondPage();
},
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
NoteSection(),
],
),
);
}
}
添加笔记屏幕
class AddNoteScreen extends StatefulWidget {
@override
_AddNoteScreenState createState() => _AddNoteScreenState();
}
final titleController = TextEditingController();
final bodyController = TextEditingController();
String noteBodyText = bodyController.text;
String noteTitleText = titleController.text;
class _AddNoteScreenState extends State<AddNoteScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Note App'),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
titleTextField,
bodyTextField,
Container(
padding: EdgeInsets.only(
right: 10,
bottom: 10,
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.amber,
),
child: Text(
'Add Note',
style: TextStyle(color: Colors.white),
),
onPressed: () {
noteTitleText = titleController.text;
noteBodyText = bodyController.text;
Navigator.pop(context,[noteTitleText,noteBodyText]);
},
),
),
],
),
);
}
}