Flutter:在 GridView 的 Tile 中使用 GestureDetector 进行点击延迟
Flutter: Tap delay with GestureDetector in a Tile of a GridView
通常情况下,以下行为本质上是快速的。但是,在 GridView(或我假设的任何 ScrollView(也尝试过 ListView))中,性能似乎很差。当我点击屏幕时,容器的不透明度会增加,但会延迟一段时间。知道我错过了什么吗?
import "package:flutter/material.dart";
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: MyWidget()
)
);
}
}
class MyWidgetState extends State<MyWidget> {
double opacity = 0.2;
@override
Widget build(BuildContext context) {
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3
),
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
setState(() {
opacity = 0.2;
});
},
onTapDown: (details) {
setState(() {
opacity = 1.0;
});
},
child: Container(
color: Colors.red.withAlpha((255 * opacity).toInt())
)
);
},
);
}
}
class MyWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyWidgetState();
}
}
也许 Listener
在你的情况下会更快
Listener(
onPointerDown: (PointerDownEvent event) {
setState(() {
opacity = 1.0;
});
},
onPointerUp: (PointerUpEvent event) {
setState(() {
opacity = 0.2;
});
},
child: Container(
color: Colors.red.withAlpha((255 * opacity).toInt())
),
)
通常情况下,以下行为本质上是快速的。但是,在 GridView(或我假设的任何 ScrollView(也尝试过 ListView))中,性能似乎很差。当我点击屏幕时,容器的不透明度会增加,但会延迟一段时间。知道我错过了什么吗?
import "package:flutter/material.dart";
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: MyWidget()
)
);
}
}
class MyWidgetState extends State<MyWidget> {
double opacity = 0.2;
@override
Widget build(BuildContext context) {
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3
),
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
setState(() {
opacity = 0.2;
});
},
onTapDown: (details) {
setState(() {
opacity = 1.0;
});
},
child: Container(
color: Colors.red.withAlpha((255 * opacity).toInt())
)
);
},
);
}
}
class MyWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return MyWidgetState();
}
}
也许 Listener
在你的情况下会更快
Listener(
onPointerDown: (PointerDownEvent event) {
setState(() {
opacity = 1.0;
});
},
onPointerUp: (PointerUpEvent event) {
setState(() {
opacity = 0.2;
});
},
child: Container(
color: Colors.red.withAlpha((255 * opacity).toInt())
),
)