skip to Main Content

i have a method _func() in HomeScreen.
i have a button in HomeScreen that navigate the user to ViewScreen from HomeScreen like this

Navigator.push(
      context,
      CupertinoModalPopupRoute(
        builder: (context) => ViewScreen(
          listItem: item,
          icon: icon,
        ),
      ),
    );

from that ViewScreen there is another button that navigates to AddEditScreen from ViewScreen like this

Navigator.of(context).pushReplacement(
      CupertinoPageRoute(
        builder: (context) => AddEditScreen(
          item: item,
        ),
      ),
    );

when i pop back from AddEditScreen i will be navigate back to HomeScreen.
and when that happens i want the _func() method in HomeScreen to run.
how can i do that?

2

Answers


  1. In your homescreen do this:

    class HomeScreen extends StatelessWidget {
      void _func() {
        // Whatever the code you have
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          // Your user interface code must come here
    
          
          ElevatedButton(
            onPressed: () {
              Navigator.push(
                context,
                CupertinoModalPopupRoute(
                  builder: (context) => ViewScreen(
                    listItem: item,
                    icon: icon,
                    callback: _func, // to Pass the callback
                  ),
                ),
              );
            },
            child: Text("Navigate to ViewScreen"),
          ),
        );
      }
    }
    

    In you Viewscreen do this:

    class ViewScreen extends StatelessWidget {
      final Function callback; 
    
      ViewScreen({required this.callback, /* other properties you want to add*/});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          // other codes
    
          // Button to navigate to AddEditScreen
          ElevatedButton(
            onPressed: () {
              Navigator.of(context).pushReplacement(
                CupertinoPageRoute(
                  builder: (context) => AddEditScreen(
                    item: item,
                    callback: callback, // Pass the callback
                  ),
                ),
              );
            },
            child: Text("Navigate to AddEditScreen"),
          ),
        );
      }
    }
    

    In your AddEditScreen do this:

    class AddEditScreen extends StatelessWidget {
      final Function callback;
    
      AddEditScreen({required this.callback, /* other properties you want to add */});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          //code that's already present
    
          
          ElevatedButton(
            onPressed: () {
              // Call the callback before popping
              callback();
    
              // Pop back to the original HomeScreen
              Navigator.pop(context);
            },
            child: Text("Pop to HomeScreen"),
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. Check out this sample example :

    import 'package:flutter/material.dart';
    
    import 'package:flutter/cupertino.dart';
    
    const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData.dark().copyWith(
            scaffoldBackgroundColor: darkBlue,
          ),
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: Center(
              child: HomeScreen(),
            ),
          ),
        );
      }
    }
    
    class HomeScreen extends StatelessWidget {
      void addFunction() {
        print(2 + 2);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(children: [
            ElevatedButton(
                onPressed: () async {
                  Navigator.push(
                    context,
                    CupertinoModalPopupRoute(
                      builder: (context) => ViewScreen(() {
                        addFunction();
                      }),
                    ),
                  );
                },
                child: const Text('Go to View screen'))
          ]),
        );
      }
    }
    
    class ViewScreen extends StatelessWidget {
      Function onbackCallback;
    
      ViewScreen(this.onbackCallback);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(children: [
            ElevatedButton(
                onPressed: () async {
                  final response = await Navigator.of(context).pushReplacement(
                    CupertinoPageRoute(
                      builder: (context) => AddEditScreen(),
                    ),
                  );
                  if (response) {
                    onbackCallback.call();
                  }
                  print(response);
                },
                child: const Text('Go to addedit screen'))
          ]),
        );
      }
    }
    
    class AddEditScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(children: [
            ElevatedButton(
                onPressed: () {
                  Navigator.of(context).pop(true);
                },
                child: const Text('Go back to home screen '))
          ]),
        );
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search