flutter 来自另一个页面的SetState在抖动中不工作

qybjjes1  于 2023-06-30  发布在  Flutter
关注(0)|答案(2)|浏览(126)

这就是我现在使用的代码。它从另一个页面调用一个自定义按钮,其中路径屏幕被更改。但是当我尝试在弹出时为原始屏幕设置状态时,它没有设置状态。

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,
          ),
    );
  }
}

自定义图标的代码是

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。当我再次保存文件时,它在应用程序中设置了状态,并进行了更改。但不是通过这个功能。如何解决这个问题?

tvz2xvvm

tvz2xvvm1#

你不能回电话。改变你拥有的地方

widget.callback!;

widget.callback!();
velaa5lx

velaa5lx2#

尝试调用如下方法:set(),而不是set

callback: set(),

相关问题