英文:
SetState from another page not working in flutter
问题
所以,这是我现在正在使用的代码。它从另一个页面调用一个自定义按钮,该按钮更改了路径屏幕。但是,当我尝试在弹出时为原始屏幕设置setState时,它不会设置状态。
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
void set() {
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomIcon(
iconLabel: 'Button',
iconPath: 'assets/icons/button.svg',
iconTitle: 'Button',
functionRoute: NewScreen.routeName,
callback: set,
),
);
}
}
自定义图标的代码如下:
class CustomIcon extends StatefulWidget {
String iconPath;
String iconLabel;
String iconTitle;
WidgetRef? ref;
String? functionRoute;
dynamic arguments;
Function? callback;
CustomIcon(
{Key? key,
this.ref,
this.onPressed,
required this.iconLabel,
required this.iconPath,
required this.iconTitle,
this.callback,
this.arguments})
: super(key: key);
@override
State<CustomIcon> createState() => _CustomIconState();
}
class _CustomIconState extends State<CustomIcon> {
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
if (widget.arguments != null) {
Navigator.pushNamed(context, widget.functionRoute!,
arguments: widget.arguments)
.then((value) {
widget.callback!();
});
} else {
Navigator.pushNamed(context, widget.functionRoute!).then((value) {
widget.callback!();
});
}
},
child: Column(
children: [
Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: kHeader2,
borderRadius: BorderRadius.circular(15),
),
child: SvgPicture.asset(
widget.iconPath,
color: Colors.white,
semanticsLabel: widget.iconLabel,
height: 100,
width: 100,
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text(
widget.iconTitle,
style: const TextStyle(fontSize: 25),
),
)
],
),
);
}
}
但是,当我弹出页面时,setState不会被调用。当我再次保存文件时,将在应用程序中进行更改。但不通过这个函数。如何修复这个问题?
英文:
So, this is the code I'm using right now. It calls for a custom button from another page where the path screen is changed. But when I try to setState for the original screen upon popping, it doesn't set state.
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
void set() {
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomIcon(
iconLabel: 'Button',
iconPath: 'assets/icons/button.svg',
iconTitle: 'Button',
functionRoute: NewScreen.routeName,
callback: set,
),
);
}
}
And the code for custom icon is
class CustomIcon extends StatefulWidget {
String iconPath;
String iconLabel;
String iconTitle;
WidgetRef? ref;
String? functionRoute;
dynamic arguments;
Function? callback;
CustomIcon(
{Key? key,
this.ref,
this.onPressed,
required this.iconLabel,
required this.iconPath,
required this.iconTitle,
this.callback,
this.arguments})
: super(key: key);
@override
State<CustomIcon> createState() => _CustomIconState();
}
class _CustomIconState extends State<CustomIcon> {
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
if (widget.arguments != null) {
Navigator.pushNamed(context, widget.functionRoute!,
arguments: widget.arguments)
.then((value) {
widget.callback!;
});
} else {
Navigator.pushNamed(context, widget.functionRoute!).then((value) {
widget.callback!;
});
}
},
child: Column(
children: [
Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: kHeader2,
borderRadius: BorderRadius.circular(15),
),
child: SvgPicture.asset(
widget.iconPath,
color: Colors.white,
semanticsLabel: widget.iconLabel,
height: 100,
width: 100,
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text(
widget.iconTitle,
style: const TextStyle(fontSize: 25),
),
)
],
),
);
}
}
But the setState is not called when I pop the page. When I save the file once again, which sets state in the app the changes are made. But not through this function. How to fix this?
答案1
得分: 3
你没有调用回调函数。将你原来的代码:
widget.callback!;
改成:
widget.callback!();
英文:
You are not calling the call back. Change where you have
widget.callback!;
to
widget.callback!();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论