SetState从另一个页面在Flutter中不起作用。

huangapple go评论68阅读模式
英文:

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() =&gt; _HomeScreenState();
}

class _HomeScreenState extends State&lt;HomeScreen&gt; {

  void set() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomIcon(
                iconLabel: &#39;Button&#39;,
                iconPath: &#39;assets/icons/button.svg&#39;,
                iconTitle: &#39;Button&#39;,
                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&lt;CustomIcon&gt; createState() =&gt; _CustomIconState();
}

class _CustomIconState extends State&lt;CustomIcon&gt; {
  @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!();

huangapple
  • 本文由 发表于 2023年6月29日 20:07:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定