Flutter 颤振gridview菜单点击功能不工作
我正在尝试创建一个最新版本的应用程序。我想创建片段而不是页面。现在在主页片段中,我尝试制作一个gridview菜单,它有6个菜单项。我被onTap功能卡住了,因为我想在点击菜单时移动另一个片段。它不起作用。我正在分享我的代码。提前谢谢Flutter 颤振gridview菜单点击功能不工作,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,我正在尝试创建一个最新版本的应用程序。我想创建片段而不是页面。现在在主页片段中,我尝试制作一个gridview菜单,它有6个菜单项。我被onTap功能卡住了,因为我想在点击菜单时移动另一个片段。它不起作用。我正在分享我的代码。提前谢谢 import 'package:flutter/material.dart'; import 'package:folder/fragments/fragment_form.dart'; class HomeFragment extends StatelessW
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])),
],
),
),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:folder/fragments/fragments_form.dart”;
类HomeFragment扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
//TODO:实现构建
退回新货柜(
高度:MediaQuery.of(context).size.height/1,
装饰:盒子装饰(
梯度:线性梯度(
开始:Alignment.topRight,
结束:对齐。左下角,
//停止:[0.1,0.5,0.7,0.9],
颜色:[
颜色。青色[50],
颜色。青色[50],
颜色。蓝色[50],
],
),
),
孩子:新中心(
子级:新建HomepageFragment(),
)
);
}
}
类HomepageFragment扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回容器(
填充:所有边缘设置(20.0),
子项:新建GridView.count(
交叉轴计数:2,
儿童:[
MyMenu(标题文字:“添加投诉”,图标图像:Icons.Add_框,图标颜色:Colors.cyan,位置:0),
MyMenu(标题文字:“登录”,图标图像:Icons.account\u框,图标颜色:Colors.cyan,位置:1),
MyMenu(标题文字:“关于应用程序”,图标图像:Icons.airplay,图标颜色:Colors.cyan,位置:2),
MyMenu(标题文字:“我们如何工作”,图标图像:Icons.alarm_on,图标颜色:Colors.cyan,位置:3),
MyMenu(标题文字:“所有投诉”,图标图像:Icons.apps,图标颜色:Colors.cyan,位置:4),
MyMenu(标题文字:“隐私”,图标图像:Icons.assistant,图标颜色:Colors.cyan,位置:5),
],
),
);
}
}
类MyMenu扩展了无状态小部件{
MyMenu({this.titleText,this.iconImage,this.iconColor,this.position});
最后一个字符串titleText;
最终Iconda iconImage;
最终材料颜色iconColor;
最终int位置;
_GetMenuItem(位置){
开关(位置){
案例0:
返回新的FormFragment();
案例1:
返回新的FormFragment();
案例2:
返回新的FormFragment();
案例3:
返回新的FormFragment();
违约:
返回新文本(“出错”);
}
}
@凌驾
小部件构建(构建上下文){
归还新卡(
颜色:颜色,白色,
保证金:所有边缘套(10.0),
孩子:InkWell(
onTap:()=>\u获取菜单项(位置),
onLongPress:()=>\u获取菜单项(位置),
颜色:颜色。绿色,
孩子:新中心(
子:新列(
mainAxisSize:mainAxisSize.min,
儿童:[
图标(
我的肖像,
尺寸:100.0,
颜色:iconColor,
),
文本(titleText,样式:新文本样式(fontSize:17,颜色:Colors.teal[800]),
],
),
),
),
);
}
}
您的\u getMenuTem
方法正在返回一个小部件
,因此您无法获得预期的beaviour
要将导航到新屏幕,您需要使用导航器
类:
示例:
Navigator.push(上下文,materialpage路径(builder:(context)=>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");
}
}
你想要什么样的行为?我想要这样,当我点击一张卡片时,它会把我带到另一个片段,比如我所包含的form fragment。我用你的函数替换了我的_getMenuTem()函数。现在出现了一个错误,════════ widgets库捕获到异常═══════════════════════════════════════════════════════ 找不到材质小部件。请尝试将您的MyMenu
小部件Card
包装在Scaffold
@mdsabbiahmedyes我尝试过了。我在另一个页面中对scafold和appbar进行了编码,因此它显示了一个错误,底部溢出,appbar正在隐藏。您能用您正在谈论的小部件更新您的问题吗?我将mymenu小部件卡包装在scafold中,所以现在appbar正在隐藏和底部溢出,但点击片段正在更改。基本上,我将scafold、appbar保存在另一个名为homepage的页面中,因此片段是打开的,而appbar是隐藏的。