2

How do I get the values from the drawer navigation pops?
Navigation push reloaded the entire page, which did not match the behavior of draw close.
Is there any way to solve this problem?

class _MyHomePageState extends State<MyHomePage> {
  String sampleText = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      endDrawer: DrawerPage(),
      body: Center(child: Text("Text returned from drawer is here $sampleText")),
    );
  }
}

class DrawerPage extends StatefulWidget {
  @override
  State<DrawerPage> createState() => _DrawerPageState();
}

class _DrawerPageState extends State<DrawerPage> {
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ElevatedButton(
        onPressed: () {
          Navigator.pop(context, "some text");
          setState(() {});
        },
        child: const Text("Back Home"),
      ),
    );
  }
}
Mindy Key
  • 25
  • 5
  • Do you want to listen to Drawer open and close function or you want to pass a value from the drawer to the home screen? – Lalit Fauzdar Mar 07 '22 at 09:32
  • I want to pass a value from the drawer to the home screen. I am having trouble getting a natural draw close animation with Navigator.push, and I am having trouble passing values with Navigator.pop. – Mindy Key Mar 07 '22 at 11:23
  • You should either use a value callback as in the answer provided below which returns a value to Main class whenever it's executed in the drawer class or you should use a `ChangeNotifier` whose value will be set/updated in the Drawer class and read in the Main class. – Lalit Fauzdar Mar 07 '22 at 18:37
  • This method cannot be used because it requires the use of a stateful widget. I don't know how to use `ChangeNotifier`, but I will look into it. – Mindy Key Mar 08 '22 at 01:16
  • You already have `StatefulWidget`, you can easily implement Callback mechanism without any issue. – Lalit Fauzdar Mar 08 '22 at 04:48

1 Answers1

3

I cannot comment yet, but I think your question is a potential duplicate.

If you look at the official documentation for the pop method, you can see that there is a parameter called result which will be the result of popped route/dialog (it has the same mechanism). Meaning that

ResultType result; // Whatever this is
Navigator.pop<ResultType>(context, result);

The problem is "detecting" when the pop is called. However, if you look at this thread, you should be able to get more information on how to detect if a drawer is closed/opened. Here is an example:

class HomeScreen extends StatelessWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Drawer test'),
      ),
      drawer: DrawerWidget(
        onOptionSelected: (int option) {
          print('Selected option: $option');
        },
      ),
    );
  }
}

class DrawerWidget extends StatelessWidget {
  final void Function(int) onOptionSelected;

  const DrawerWidget({
    Key? key,
    required this.onOptionSelected,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        children: [
          ListTile(
            title: const Text('first option'),
            onTap: () {
              onOptionSelected(1);
              Navigator.pop(context);
            },
          ),
          ListTile(
            title: const Text('second option'),
            onTap: () {
              onOptionSelected(2);
              Navigator.pop(context);
            },
          ),
          ListTile(
            title: const Text('third option'),
            onTap: () {
              onOptionSelected(3);
              Navigator.pop(context);
            },
          ),
        ],
      ),
    );
  }
}
sunderee
  • 189
  • 1
  • 11