【问题标题】:Flutter BottomSheet setstate cannot refresh main pageFlutter BottomSheet setstate 无法刷新主页
【发布时间】:2022-07-23 22:29:45
【问题描述】:

即使使用 statefulwidget,bottomsheet 中的 setstate 也无法正常工作...我想在弹出 bottomsheet 后刷新父级的显示

我应该在 void 中调用 setstate 吗?如何做到这一点?

ElevatedButton(
             ...
   onPressed: () {
                  BottomSheet(context, id, expiry);
                  },
             ...
),


void BottomSheet(BuildContext context, String id, String expiry) {
  showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    ....
        ),
      ),
      child: BottomSheetWidget(id, expiry),
    ),
  );
}


class BottomSheetWidget extends StatefulWidget {
  ....
}

class _BottomSheetWidgetState extends State<BottomSheetWidget> {
  ..
                onTap: () {
                  setState(() {
                    updated parent data here.....
                  });
                  Navigator.pop(context);
                  },
                ....
}

【问题讨论】:

  • 你能包含完整的示例小部件吗?
  • 检查这个链接也许你的问题是一样的。 refreshing a page on pop

标签: android flutter flutter-showmodalbottomsheet


【解决方案1】:

尝试使用MaterialPageRoute重建父页面的显示

onTap: () {
  setState(() {
     updated parent data here.....
   });
  Navigator.pushReplacement(
    context,
    MaterialPageRoute(builder: (context) => ParentScreen()),
  );
}

【讨论】:

    猜你喜欢
    • 2021-03-02
    • 1970-01-01
    • 2020-12-05
    • 2021-10-25
    • 2023-04-05
    • 2021-09-08
    • 1970-01-01
    • 2018-06-11
    • 2021-09-06
    相关资源
    最近更新 更多