【问题标题】:Redirect menuitem to another page将菜单项重定向到另一个页面
【发布时间】:2021-01-15 21:26:27
【问题描述】:

我有这个创建菜单项的功能,但它没有任何功能,我想要的是这样我可以在声明它时将它发送到我想要的页面。我该怎么做?

final List<MenuItem> options = [
MenuItem(Icons.home, 'Home'),
MenuItem(Icons.person, 'Profile'),
MenuItem(Icons.web_asset, 'Website'),
MenuItem(Icons.settings, 'Settings'),

];

             children: options.map((item) {
            
            return ListTile(
              
              onTap: () {
            Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => HERE I WANT TO PUT A CUSTOM PAGE FOR EACH ONE()),
                );
          },
              leading: Icon(
                item.icon,
                color: Colors.white,
                size: 35,
              ),
              title: Text(
                item.title,
                style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.bold,
                    color: Colors.white),
              ),
            );
          }).to

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    添加功能 OnTap;到您的模型如下:

    class MenuItem{
      Icon icon;
      String title;
      Function OnTap;
    
      MenuItem(this.icon, this.title, this.OnTap);
    }
    

    然后添加新对象;

    final List<MenuItem> options = [
    MenuItem(Icons.home, 'Home',(){print("define_onclick_here");}),
    MenuItem(Icons.person, 'Profile',(){print("define_onclick_here");}),
    MenuItem(Icons.web_asset, 'Website',(){print("define_onclick_here");}),
    MenuItem(Icons.settings, 'Settings',(){print("define_onclick_here");}),];
    

    最后在列表项 onTap 中添加item.OnTap();

      children: options.map((item) {
            
            return ListTile(
              
              onTap: () {
            item.OnTap();
          },
              leading: Icon(
                item.icon,
                color: Colors.white,
                size: 35,
              ),
              title: Text(
                item.title,
                style: TextStyle(
                    fontSize: 14,
                    fontWeight: FontWeight.bold,
                    color: Colors.white),
              ),
            );
          }).to
    

    【讨论】:

    • 一件事。我将它添加到 define_onclick_here ("""Navigator.push( context, MaterialPageRoute(builder: (context) => Profile()), );""") 并且没有任何反应。我应该在那里添加什么?
    • @AndreiMarin use (){ Navigator.push( context, MaterialPageRoute(builder: (context) => Profile())); }
    • 您知道为什么我会收到错误“未识别的名称上下文”吗? gyazo.com/ff7c46856e2bf15b7d60910dccfb2e87
    • @AndreiMarin 可能是您没有访问此文件中的上下文,请阅读以下页面:stackoverflow.com/questions/52962112/…
    猜你喜欢
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多