【发布时间】:2022-01-10 17:58:50
【问题描述】:
在我的应用程序中,我在MaterialApp 上方定义了一个提供程序Song,以便通过我的应用程序访问它。
需要:我这样做是因为我有一个PageA,其中包含一个songTitle 变量和一个转到PageB 的按钮。
在PageB 上,我有一个按钮调用我的提供者Song 并更新PageA。因此,当我在PageB 上执行Navigator.pop (context) 时,我会回到PageA 并查看更新后的songTitle 变量。
为了能够从PageB 更新PageA,我必须将我的提供者Song 放在MaterialApp 上方。
==> 有效。
我的问题:我希望能够在致电PageA 时重置我的提供程序。因此,如果我的songTitle 变量已更新并且我退出pageA,我希望我的songTitle 变量在我初始化提供程序Song 时返回其默认值。目前songTitle 变量始终保持更新...
这里是路由器:
abstract class RouterClass{
static Route<dynamic> generate(RouteSettings settings){
final args = settings.arguments;
switch(settings.name){
case RouterName.kMenu:
return CupertinoPageRoute(
builder: (context) => Menu()
);
case RouterName.kPageA:
return CupertinoPageRoute(
builder: (context) => PageA()
);
case RouterName.kPageB:
return CupertinoPageRoute(
builder: (context) => PageB()
);
default:
return CupertinoPageRoute(
builder: (context) => Error404View(title: "Error")
);
}
}
}
菜单:
class Menu extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Menu'),
),
body : Center(
child: MaterialButton(
onPressed: () {
Navigator.pushNamed(
context,
RouterName.kPageA,
),
},
child: Text('Button'),
),
),
),
);
}
}
页面A:
class PageA extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('pageA'),
),
body : Center(
child: Consumer<Song>(builder: (context, song, child) {
print('Consumer() : ${song.songTitle}');
return Column(
children: <Widget>[
// SONG TITLE
Text(song.songTitle),
// Button
MaterialButton(
onPressed: () => Navigator.pushNamed(
context,
RouterName.kPageB,
),
child: Text('Button'),
),
],
);
}),
),
),
);
}
}
PageB:
class PageB extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('pageB'),
),
body : Center(
child: MaterialButton(
onPressed: () {
Provider.of<Song>(context, listen: false).updateSongTitle('New Title');
},
child: Text('Button'),
),
),
),
);
}
}
提供者宋:
class Song extends ChangeNotifier {
late String songTitle;
Song(){
_initialise();
}
Future _initialise() async
{
songTitle = "Title";
notifyListeners();
}
void updateSongTitle(String newTitle) {
songTitle = newTitle;
notifyListeners();
}
}
【问题讨论】: