Dart / Flutter: Navigate (with Navigator) between Pages without context (so no Navigator.push?)

702

Solution 1

You use a GlobalKey for the navigator. There are already a lot of articles describing how to do it, here is an example

Solution 2

Another option is to wrap your favoriteContainerLists in a single Widget (maybe the column that wraps it) and use a build function. So you might have something like:

class FavoriteContainerList extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Column(
        child: The logic from AppBrain().favoriteContainerLists() here.

This other SO answer on the difference between functions and classes convinced me to use classes for other reasons, but getting context is nice too.

Share:
702
PhiJoTo
Author by

PhiJoTo

Updated on December 24, 2022

Comments

  • PhiJoTo
    PhiJoTo over 1 year

    In my App I have 6 different pages. Theres 1 page called "FavouritesPage", where user pickes favourites are listed. They are displayed by a column, which uses a list from another class. The code of the list is following:

      List<Widget> favoriteContainerLists() {
        DatabaseProvider.db.queryDb();
        List<Widget> localList = [];
        if (DatabaseProvider.publicFavoriteList == null ||
            DatabaseProvider.publicFavoriteList.isEmpty) {
          localList.add(
            Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  "Hier erscheinen deine Favoriten!",
                  style: TextStyle(
                    fontFamily: "Hind",
                    fontSize: 22,
                  ),
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "Makiere Bilder auf der Informationsseite mit ",
                      style: TextStyle(
                        color: Colors.indigo,
                        fontFamily: "Hind",
                        fontSize: 15,
                      ),
                    ),
                    Icon(
                      Icons.star_border,
                      color: Colors.orange,
                    ),
                  ],
                ),
                Text(
                  " um sie hinzuzufügen!",
                  style: TextStyle(
                    color: Colors.indigo,
                    fontFamily: "Hind",
                    fontSize: 15,
                  ),
                ),
              ],
            ),
          );
        } else {
          localList.add(Padding(
            padding: const EdgeInsets.only(bottom: 15.0),
            child: Text(
              "Deine Favoriten:",
              textAlign: TextAlign.left,
              style: TextStyle(
                fontSize: 30,
                color: Colors.indigoAccent,
                fontFamily: "Hind",
              ),
            ),
          ));
          for (var i = 0; i < DatabaseProvider.publicFavoriteList.length; i++) {
            DatabaseProvider.db.queryDb();
            int currentIdInt = DatabaseProvider.publicFavoriteList[i];
            localList.add(
              Padding(
                padding: const EdgeInsets.only(bottom: 15),
                child: MaterialButton(
                  onPressed: ***null***,
                  padding: const EdgeInsets.only(bottom: 0),
                  child: Container(
                    width: double.infinity,
                    height: 120,
                    decoration: BoxDecoration(
                      color: buttonColor,
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(8),
                          topRight: Radius.circular(8),
                          bottomLeft: Radius.circular(8),
                          bottomRight: Radius.circular(8)),
                      boxShadow: [
                        BoxShadow(
                          color: Colors.grey.withOpacity(0.5),
                          spreadRadius: 5,
                          blurRadius: 7,
                          offset: Offset(0, 3), // changes position of shadow
                        ),
                      ],
                    ),
                    child: Padding(
                      padding: const EdgeInsets.fromLTRB(10, 8, 10, 8),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Center(
                            child: Container(
                              width: 70,
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(8),
                                    topRight: Radius.circular(8),
                                    bottomLeft: Radius.circular(8),
                                    bottomRight: Radius.circular(8)),
                              ),
                              child: Image.asset(
                                  AppBrain().contentList[currentIdInt].imageAdress),
                            ),
                          ),
                          VerticalDivider(
                            thickness: 2,
                            indent: 15,
                            endIndent: 15,
                          ),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Text(
                                  AppBrain().contentList[currentIdInt].artName,
                                  style: TextStyle(
                                      fontFamily: "Hind",
                                      fontSize: 23,
                                      color: Colors.blueGrey),
                                ),
                                Expanded(
                                  child: Container(
                                    child: RichText(
                                      maxLines: 2,
                                      softWrap: true,
                                      overflow: TextOverflow.ellipsis,
                                      textAlign: TextAlign.left,
                                      text: TextSpan(
                                        style: TextStyle(
                                            fontSize: 14,
                                            color: Colors.black,
                                            fontFamily: "Hind"),
                                        text:
                                            (contentList[currentIdInt].description),
                                      ),
                                    ),
                                  ),
                                )
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
            );
          }
        }
        return localList;
      }
    

    The code of the page displaying it:

      Widget build(BuildContext context) {
        DatabaseProvider.db.queryDb();
        return SafeArea(
          child: Scaffold(
            backgroundColor: Colors.white,
            bottomNavigationBar: BottomNavigationBar(
              items: [
                BottomNavigationBarItem(
                    icon: Icon(Icons.home), title: Text("Startseite")),
                BottomNavigationBarItem(
                    icon: Icon(Icons.star), title: Text("Favoriten")),
                BottomNavigationBarItem(
                    icon: Icon(Icons.casino), title: Text("Quiz")),
                BottomNavigationBarItem(
                    icon: Icon(Icons.map), title: Text("Karte")),
              ],
              type: BottomNavigationBarType.fixed,
              unselectedItemColor: Colors.grey,
              showUnselectedLabels: true,
              currentIndex: _selectedIndex,
              selectedItemColor: Colors.blue,
              onTap: _onItemTapped,
            ),
            body: SingleChildScrollView(
              child: WillPopScope(
                onWillPop: _backButtonPressed,
                child: Padding(
                  padding: const EdgeInsets.only(left: 15, right: 15, top: 8),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: AppBrain().favoriteContainerLists(),
                  ),
                ),
              ),
            ),
          ),
        );
      }
    

    They are located in 2 different classes. The class containing the list doesn't have a BuildContext and also should have non. Ideally, where it says onTap: null, at the moment I want it to be

    Navigator.of(context).push(
         toInformationPage(),
                              ); 
    

    but I know I can't use anything that requires context. Any Ideas?