如何在警报对话框按钮上重新加载屏幕按下?
我正在制作一个应用程序,该应用显示来自子reddit的图像,我想根据通过警报对话框输入的子reddit名称更改这些图像。我已经实施了如下所有内容,但是当我设置状态时,图像没有显示。如何解决这个问题?这是我的代码。
import 'package:flutter/material.dart';
import 'package:memes/screens/memes_screen.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
)
);
}
class HomePage extends StatefulWidget {
const HomePage({ Key? key }) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final TextEditingController textEditingController = TextEditingController();
String subs = "memes";
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromARGB(255, 255, 254, 241), //Color(0xffdedede),
appBar: AppBar(title: Text("Memes"), backgroundColor: Color(0xff008b00)),
body: MemesScreen(subreddits: subs),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
backgroundColor: Color(0xff008b00),
onPressed: (){
_showDialog();
},
),
);
}
void _showDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
backgroundColor: Color.fromARGB(255, 255, 254, 241),
title: Text("Enter subreddits"),
content: TextField(
controller: textEditingController,
),
actions: <Widget>[
// usually buttons at the bottom of the dialog
TextButton(
child: new Text("Close", style: TextStyle(color: Color(0xff008b00))),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: new Text("Get", style: TextStyle(color: Color(0xff008b00))),
onPressed: () {
String newSubs = textEditingController.text;
setState(() {
subs = newSubs;
});
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
字符串subs
传递给显示图像的semesscreen
。如果我更改此subs
在按钮上的值按_showdialog
方法中的方法并设置状态,则屏幕没有重新加载。如何解决这个问题?任何帮助将不胜感激。
I'm making an app that shows images from a subreddit and I want to change these images based on the subreddit names entered through the alert dialog. I have implemented everything as follows but when I set the state, the images are not showing. How to fix this? Here is my code.
import 'package:flutter/material.dart';
import 'package:memes/screens/memes_screen.dart';
void main() {
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
)
);
}
class HomePage extends StatefulWidget {
const HomePage({ Key? key }) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final TextEditingController textEditingController = TextEditingController();
String subs = "memes";
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromARGB(255, 255, 254, 241), //Color(0xffdedede),
appBar: AppBar(title: Text("Memes"), backgroundColor: Color(0xff008b00)),
body: MemesScreen(subreddits: subs),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
backgroundColor: Color(0xff008b00),
onPressed: (){
_showDialog();
},
),
);
}
void _showDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
backgroundColor: Color.fromARGB(255, 255, 254, 241),
title: Text("Enter subreddits"),
content: TextField(
controller: textEditingController,
),
actions: <Widget>[
// usually buttons at the bottom of the dialog
TextButton(
child: new Text("Close", style: TextStyle(color: Color(0xff008b00))),
onPressed: () {
Navigator.of(context).pop();
},
),
TextButton(
child: new Text("Get", style: TextStyle(color: Color(0xff008b00))),
onPressed: () {
String newSubs = textEditingController.text;
setState(() {
subs = newSubs;
});
Navigator.of(context).pop();
},
),
],
);
},
);
}
}
The string subs
is passed to the MemesScreen
that shows an image. If I change this subs
value on a button press in _showDialog
method and set the state, the screen is not reloading. How to fix this? Any help would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将semesscreen(subreddits:subs)包装到valuelistenablebuilder()中。然后将字符串subs变量放入Globals.dart页面,以便可以在SetState上更改它。
编辑您的代码如下
对话框页面SETSTATE将是
Globals
。
main.dart
meme_screen.dart
globals.dart
另一种刷新页面的方法是:
使用navigator.pushreplacement()代替navigator.of(context).pop()。
这是_showdialog()的代码:
它将重新加载主页。但是最后一个堆栈丢失意味着您不能回到最后一页。
Wrap MemesScreen(subreddits: subs) INTO ValueListenableBuilder(). Then Put String subs variable in globals.dart page so that it can be changed on setState.
Edit your Code as below
Dialog page setState will be
globals.dart will be
Check this full code working and tested:
main.dart
meme_screen.dart
globals.dart
Another method of refreshing page is:
use Navigator.pushReplacement() instead of Navigator.of(context).pop().
This is the code of _showDialog():
It will reload the HomePage. But the last stack was lost means you cannot go back on last page.