通常,以下行为本质上是快速的。但是,在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();
}
}
答案 0 :(得分:2)
在您的情况下,也许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())
),
)