Navigation between screens in Flutter

Asked

Viewed 184 times

0

I’m making an app where I need to open navigate to a new screen when I click on some Gridview container, it was working normally when the containers were each on an Inkwell, but the code is getting very confusing so I decided to optimize, but now I’m not able to call a new screen in Navigator. NOTE: For now I only made 2 They so I called for the same screen in all buildGridView.

                          Container(
                            width: MediaQuery.of(context).size.width,
                            height: MediaQuery.of(context).size.height * 0.5,

                            child: GridView.count(
                              crossAxisCount: 2,
                              crossAxisSpacing: 25,
                              mainAxisSpacing: 25,
                              padding: const EdgeInsets.all(20),

                              children: <Widget>[
                                buildGridView("Quartos",context, 
                                RoomsScreen()),
                                buildGridView("Sala 
                                Principal",context,RoomsScreen()),
                              buildGridView("Cozinha",context,RoomsScreen()),
                               buildGridView("Garage",context,RoomsScreen()),
                                buildGridView("Área 
                                Externa",context,RoomsScreen()),
                            buildGridView("Adicionar",context,RoomsScreen()),
                              ],
                            ),
                          )
                        ])))],
                              ),
                                   ],
                                        ),
                                       );
                                       }
                                      }
                  Widget buildGridView(String text, BuildContext context, 
                 String page,){
                  return InkWell(
                   borderRadius: BorderRadius.circular(15),
                   highlightColor: Colors.red,
                     onTap: () {
                     Navigator.push(context, MaterialPageRoute(builder: 
                  (context) => page));
                        },
                   child: Container(
                         decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15),
                            border: Border.all(
                                 color: Colors.black,
                                width: 2,
                                     )),
                                 padding: EdgeInsets.all(30),
                                   child: Column(
                             mainAxisAlignment: MainAxisAlignment.center,
                                    children: <Widget>[
                                          Text(
                                         text,
                                     style: TextStyle(
                                         color: Colors.white,
                                          fontWeight: FontWeight.bold,
                                            fontSize: (15)),
                                                    )
                                                     ])));

                                                         }
  • Prefer to use routes with Name instead of passing a Widget every time to Materialpage, as you say the code "was" actually still is, remember in Flutter everything is a Widget, debt your code in small Widgets/Methods that do the job in a simple way and that is easy to read.

  • Thank you I did that you said. now is much better.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.