flutter - Dart/Flutter : Navigate (with Navigator) between Pages without context (so no Navigator. 推送?)

标签 flutter dart flutter-layout flutter-test

在我的应用程序中,我有 6 个不同的页面。 有 1 个页面称为“FavouritesPage”,其中列出了用户选择的收藏夹。它们通过一列显示,该列使用另一个类的列表。列表代码如下:

  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;
  }

显示它的页面代码:

  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(),
              ),
            ),
          ),
        ),
      ),
    );
  }

它们位于 2 个不同的类别。包含列表的类没有 BuildContext,也应该没有。理想情况下,它显示 onTap: null, 此时我希望它是

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

但我知道我不能使用任何需要上下文的东西。有什么想法吗?

最佳答案

您使用 GlobalKey 作为导航器。已经有很多文章描述了如何做到这一点,这里是an example

关于flutter - Dart/Flutter : Navigate (with Navigator) between Pages without context (so no Navigator. 推送?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63962860/

相关文章:

firebase - 使用 map 的 Flutter Firestore where 子句

flutter - 如何在 Flutter 中更新 pubspec.yaml 文件中的单个包

Flutter:在运行时将 String 转换为类字段名称

flutter - 如何在 Flutter 中实现应用栏操作文本而不是操作图标?

firebase - 尝试实现Cloud Firestore时,Flutter构建失败,卡在 'pod install’中

android - 使用SingleChildScrollView时的堆栈小部件错误

flutter - 做类类型别名的更好方法?

flutter - 如何为整个 child (除一个 flutter 中的选定 child )禁用点击事件?

flutter - 切换按钮, flutter : How to change border color and border radius

flutter - 在 flutter 中格式化文本小部件的部分文本