【发布时间】: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