简体   繁体   English

从 flutter 中的另一个屏幕返回时如何调用方法?

[英]How to call a method when navigating back from another screen in flutter?

I have a list view of items.我有一个项目列表视图。 For each item in the list, there is an edit option, when on click that im navigating from ListScreen to the EditScreen.对于列表中的每个项目,都有一个编辑选项,单击时从 ListScreen 导航到 EditScreen。 Edit Screen has a static update button but the editing view is changed according to the item selected to be edited From the ListScreen.编辑屏幕有一个 static 更新按钮,但编辑视图会根据从 ListScreen 中选择要编辑的项目进行更改。

Now when I update the items in Edit screen, and press update button I can navigate to ListScreen back but I need the list view to rebuild on that.现在,当我更新编辑屏幕中的项目并按下更新按钮时,我可以导航到 ListScreen,但我需要重建列表视图。 The function to build the list is given inside of a FutureBuilder in ListScreen it self.用于构建列表的 function 在其自身的 ListScreen 中的 FutureBuilder 内部给出。 In the initial navigate to the ListScreen im calling that method in initState().在初始导航到 ListScreen 我在 initState() 中调用该方法。

Is there a way that I can manage this?有没有办法我可以管理这个?

ListScreen列表屏幕

//provider
late AddItemProvider? _updateItemProvider;

@override
  void initState() {
    _dataFuture = _getAddedData().whenComplete(() => refresh());
    super.initState();
  }

void _gotoEditAccess(BuildContext ctx, String title) async {
    var nav =
        Navigator.pushNamed(ctx, suggestionUpdateUIRoute, arguments: title);
    // of(context)
    if (nav != null && nav == true) {
      await _dataFuture; //_getAddedData().whenComplete(() => refresh());
    }
  }

//in the body I have this list view
//added item is the list view item which holds Function parameters for onEdit and onDelete
//used as onTap: () => widget.onEdit!(),

ListView.builder(
                        shrinkWrap: true,
                        itemCount: _listOfItems.length,
                        itemBuilder: (context, index) {
                          return AddedItem(
                            icon: _listOfItems[index].icon,
                            title: _listOfItems[index].title,
                            content: _listOfItems[index].content,
                            onEdit: () async {
                              _gotoEditAccess(
                                  context, _listOfItems[index].title!);
                            },
                            onDelete: () {
                              _deleteItem(_listOfItems[index].title!);
                            },
                          );
                        },
                      ),

edit screen have a base view with common update button and the body is being replaced with a actual editing view matching what needs to be edited.编辑屏幕有一个带有通用更新按钮的基本视图,并且正文被替换为与需要编辑的内容相匹配的实际编辑视图。

EditScreen编辑画面

@override
  Widget build(BuildContext context) {
    _updateItemProvider = Provider.of<AddItemProvider>(context, listen: false);
...
//rest of the body for matched editing screen...

//inside the base view of edit screen

_goBack() {
    Navigator.pop(context, true);}

@override
  Widget build(BuildContext context) {
    _updateItemProvider = Provider.of<AddItemProvider>(context, listen: false);

_submitBtn() => Consumer<AddItemProvider>(
          builder: (_, AddItemProvider updateItemProvider, __) {
            _updateItemProvider = updateItemProvider;
            return ButtonWidget(
              btnColor: CustomColors.green600,
              borderColor: CustomColors.green600,
              textColor: CustomColors.mWhite,
              text: "Update",
              eButtonType: eButtonType.bText,
              eButtonState: _submitBtnState,
              onPressed: () async {
                await saveSelectedList(updateItemProvider.updateItems!);
                _updateItemProvider!.removeAll();
                _goBack();
              },
            );
          },
        );

You need to write this code were you want to navigate如果您想导航,您需要编写此代码

onTap:() async{
                var test = await Navigator.of(context).push(
                          MaterialPageRoute(builder: (context) =>EditProfile()));
   
                if(test != null || test == true){
                  // perform your function
                 }
           }

You need to pass any content when navigate back from edit screen从编辑屏幕返回时,您需要传递任何内容

 Navigator.pop(context, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 bloc flutter 从屏幕 B 导航回来时如何更新屏幕 A - How to update screen A when navigating back from screen B with bloc flutter flutter 从另一个屏幕返回屏幕后呼叫 function - flutter call a function after moving back to screen from another screen 从侧面菜单 flutter 导航时如何清除返回堆栈? - How to clear back stack when navigating from side menu flutter? 更新Firestore文档时,颤动屏幕向后导航 - Flutter screen navigating back when updating firestore document 关闭关于从第二个屏幕导航回来并刷新页面的 AlertDialog flutter - Close AlertDialog on Navigating back from second screen and refresh page flutter Flutter:从另一个回来时更新屏幕 - Flutter: update screen when coming back from another 如何在 flutter 导航回上一屏幕后显示弹出窗口? - How to display popup after navigating back to previous screen in flutter? Flutter 提供程序在导航到另一个屏幕时失去价值 - Flutter provider loosing value when navigating to another screen 使用riverpod导航到flutter中的另一个屏幕时如何保留应用程序的state - How to retain the state of the app when navigating to another screen in flutter using riverpod 如何从 flutter 中的另一个 class 调用方法 - How to call a method from another class in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM