gpt4 book ai didi

flutter - 使 GridView 中的列在 Flutter 中可点击

转载 作者:IT王子 更新时间:2023-10-29 06:41:59 40 4
gpt4 key购买 nike

我想让我的 GridView 的各个列都可以点击。我不太明白如何使用 GestureDetector/InkWell 做到这一点。我不明白如何访问网格的整列。

我该怎么做(如果这甚至可以使用 GridView 实现)?如果不可能,我能做到这一点的最佳方法是什么?

GridView.count(
crossAxisCount: 10,
children: List.generate(
50,
(_) {
return Container(
color: mainColor,
child: Container(
decoration: BoxDecoration(
color: Colors.white60,
shape: BoxShape.circle,
),
),
);
},
),
)

最佳答案

通过小时候使用 InkWell 和一点点数学:

enter image description here

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
final appTitle = 'Column selecion demonstration';

@override
Widget build(BuildContext context) {
return MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}

class MyHomePage extends StatefulWidget {
final String title;

MyHomePage({Key key, this.title}) : super(key: key);

@override
_MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State<MyHomePage> {

int selectedIndex = -1;
int columnsCount = 10 ;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: GridView.count(
crossAxisCount: columnsCount,
children: List.generate(
50, ( index ) {
return InkWell(
onTap: (){
setState(() {
if(selectedIndex != index){
selectedIndex = index ;
}else{
selectedIndex = -1 ;
}
});
},
child: Container(
color: (selectedIndex % columnsCount == index % columnsCount) && selectedIndex != -1 ? Colors.blue : Colors.yellow,
child: Container(
decoration: BoxDecoration(
color: Colors.white60,
shape: BoxShape.circle,
),
),
),
);
},
),
),
);
}
}

关于flutter - 使 GridView 中的列在 Flutter 中可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57615427/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com