flutter - 创建具有多个可点击项目(类别)的网格视图
问题描述
我想制作一个具有十二个类别的多选网格视图。我无法创建它的可点击和不可点击部分。另外,我怎样才能让用户可以选择多个类别?正如您在下面的代码中看到的那样,我希望用户根据他们的兴趣在网格中单击大约十二个类别,这就是需要多选的原因。
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: const Text('Interests'),
),
resizeToAvoidBottomPadding: false,
body: GridView.count(
padding: const EdgeInsets.all(30),
childAspectRatio: (6 / 1),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
// Create a grid with 1 columns.
crossAxisCount: 1,
children: <Widget> [
Container(
padding: const EdgeInsets.all(8),
child: const Text('Food & Drinks'),
color: Colors.teal[100],
),
Container(
padding: const EdgeInsets.all(1),
child: const Text('Sport'),
color: Colors.teal[200],
),
Container(
padding: const EdgeInsets.all(8),
child: const Text('Fitness'),
color: Colors.teal[300],
),
Container(
padding: const EdgeInsets.all(8),
child: const Text('Animals'),
color: Colors.teal[100],
),
Container(
padding: const EdgeInsets.all(1),
child: const Text('Parent hang'),
color: Colors.teal[200],
),
Container(
padding: const EdgeInsets.all(8),
child: const Text('Film'),
color: Colors.teal[300],
),
Container(
padding: const EdgeInsets.all(8),
child: const Text('Explore'),
color: Colors.teal[100],
),
Container(
padding: const EdgeInsets.all(1),
child: const Text('Nature'),
color: Colors.teal[200],
),
Container(
padding: const EdgeInsets.all(8),
child: const Text('Gaming'),
color: Colors.teal[300],
),
Container(
padding: const EdgeInsets.all(8),
child: const Text('Music'),
color: Colors.teal[100],
),
Container(
padding: const EdgeInsets.all(1),
child: const Text('Art'),
color: Colors.teal[200],
),
Container(
color: Colors.green,
padding: const EdgeInsets.all(8),
child: Center(
child: Text('Culture events', style: TextStyle(color: Colors.black)
),
]
)
);
}
}
解决方案
You need to surround each Container
with an Inkwell
(if you want a ripple effect on click) or a GestureDetector
(if you just want a simple tap).
After that, you have to initialise a structure that keeps track of whether the item is clicked or not. For example, an array of boolean values which has the corresponding index as true
if the corresponding item is clicked.
In the onTap()
method of your surrounding Inkwell
/GD
, set the value in the structure you made above. Also, add an indication about the selection, like invert colors of the text and background, or change the background color if the item is selected vs not selected.
推荐阅读
- python - 如何从 Qdialog 窗口返回
- python - 无法在 Gcloud 上部署 Django 项目
- java - 从 Scala 访问 HBase 时发生了非法反射访问操作
- javascript - Wordpress 使用 javascript 更改 iframe src
- c# - Hangfire、EF dbContext 和 JSON 参考循环
- android - 在深度打盹模式测试中未收到 FCM 高优先级消息
- javascript - 在 JS/ES6 中获取枚举键的文字字符串
- amazon-web-services - 如何创建一个蟒蛇蛋?并火花提交
- javascript - Javascript 对象变量常量
- mongodb - 如何优化聚合查询以根据不同的键进行分组?