Search code examples
flutternavigationsetstate

How can use Navigator with isSelected by flutter


enter image description here

enter image description here

Relating to isSelected and Navigator, how can I use these function together?

Now I have some error happened...

add_page.dart

class _AddPage extends StatefulWidget {

  @override   State<StatefulWidget> createState() => AddPage();

}

main.dart

        return FloatingActionButton(
          onPressed: () async {
            await Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => AddPage(model),
              ),
            );
          },

Solution

  • If you want to pass the MainModel model to your Addpage class, you could just pass it as such:

    class AddPage extends StatefulWidget {
      final MainModel model;
    
      const AddPage(this.model);
    
      @override
      _AddPageState createState() => _AddPageState();
    }
    
    class _AddPageState extends State<AddPage> {
      @override
      Widget build(BuildContext context) {
        // Your widget tree
        // ...
    
      }
    }
    

    But you could just push the AddPage screen as you did, and retrieve the MainModel model with a Consumer from your context, as you already did.