【问题标题】:Flutter gridview menu on tap function is not working点击功能上的颤振 gridview 菜单不起作用
【发布时间】:2020-12-10 05:22:15
【问题描述】:

我正在尝试在 Flutter 最新版本中创建应用程序。我想创建片段而不是页面。现在在主页片段中,我尝试制作一个有 6 个菜单项的 gridview 菜单。我被困在 onTap 功能上,因为我想在点击菜单时移动另一个片段。它不工作。我正在分享我的代码。提前致谢。

import 'package:flutter/material.dart';
import 'package:folder/fragments/fragment_form.dart';

class HomeFragment extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Container(
        height: MediaQuery.of(context).size.height / 1,
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topRight,
            end: Alignment.bottomLeft,
            //stops: [0.1, 0.5, 0.7, 0.9],
            colors: [
              Colors.teal[50],
              Colors.cyan[50],
              Colors.blue[50],
            ],
          ),
        ),
        child: new Center(
          child: new HomepageFragment(),
        )
    );
  }

}

class HomepageFragment extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(20.0),
      child: new GridView.count(
        crossAxisCount: 2,
        children: <Widget>[
          MyMenu(titleText: "Add Complain", iconImage: Icons.add_box, iconColor: Colors.cyan, position: 0),
          MyMenu(titleText: "Log In", iconImage: Icons.account_box, iconColor: Colors.cyan, position: 1),
          MyMenu(titleText: "About App", iconImage: Icons.airplay, iconColor: Colors.cyan, position: 2),
          MyMenu(titleText: "How we Work", iconImage: Icons.alarm_on, iconColor: Colors.cyan, position: 3),
          MyMenu(titleText: "All Complains ", iconImage: Icons.apps, iconColor: Colors.cyan, position: 4),
          MyMenu(titleText: "Privacy", iconImage: Icons.assistant, iconColor: Colors.cyan, position: 5),
        ],
      ),
    );
  }
}

class MyMenu extends StatelessWidget {

  MyMenu({this.titleText, this.iconImage, this.iconColor, this.position});
  final String titleText;
  final IconData iconImage;
  final MaterialColor iconColor;
  final int position;


  _getMenuTem(position){
    switch (position) {
      case 0:
        return new FormFragment();
      case 1:
        return new FormFragment();
      case 2:
        return new FormFragment();
      case 3:
        return new FormFragment();

      default:
        return new Text("Something went wrong");
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Card(
      color: Colors.white,
      margin: EdgeInsets.all(10.0),
      child: InkWell(
        onTap: () => _getMenuTem(position),
        onLongPress: () => _getMenuTem(position),
        splashColor: Colors.green,
        child: new Center(
          child: new Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Icon(
                iconImage,
                size: 100.0,
                color: iconColor,
              ),
              Text(titleText, style: new TextStyle(fontSize: 17, color: Colors.teal[800])),

            ],
          ),
        ),
      ),
    );
  }
}

【问题讨论】:

  • 你想要什么样的行为?
  • 我希望,当我点击一张卡片时,它应该将我带到另一个片段,例如我包含的表单片段。

标签: flutter dart flutter-layout


【解决方案1】:

您的 _getMenuTem 方法返回 Widget,因此您无法获得预期的行为。

TOnavigate 到一个新屏幕,你需要使用Navigator 类:

示例: Navigator.push(context,MaterialPageRoute(builder: (context) =&gt; NewScreen()));

我以你的代码为例添加了一个演示:

_getMenuTem(position, context) {
    switch (position) {
      case 0:
        // navigate to specific screen
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => FormFragment(),
          ),
        );
        break;
      case 1:
        // navigate to specific screen
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => FormFragment(),
          ),
        );
        break;
      case 2:
        // navigate to specific screen
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => FormFragment(),
          ),
        );
        break;
      case 3:
        // navigate to specific screen
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => FormFragment(),
          ),
        );
        break;
      default:
        return new Text("Something went wrong");
    }
  }

【讨论】:

  • 我用你的函数替换了我的 _getMenuTem() 函数。现在显示错误,════════ 小部件库捕获的异常══════════════════════════════ ════════════════════════ 找不到材质小部件。
  • 尝试将您的 MyMenu 小部件 Card 包装在 Scaffold @MdSabbirAhmed 中
  • 是的,我试过了。我在另一个页面中编写了 scafold 和 appbar,所以它显示错误,底部溢出和 appbar 被隐藏。
  • 你能用你正在谈论的小部件更新你的问题吗?
  • 我将 mymenu 小部件卡片包裹在支架中,所以现在 appbar 隐藏并且底部溢出,但点击片段正在改变。基本上我将支架、appbar 保留在另一个称为主页的页面中,因此片段正在打开但 appbar 隐藏。
猜你喜欢
  • 2022-08-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2023-02-08
  • 2017-05-29
  • 2017-11-14
相关资源
最近更新 更多