[英]Listview not scrolling with swipe gesture on listview Flutter
[英]Flutter vertical swipe and avoiding scrolling Listview
您可以使用GestureDetector
包裝圖像小部件,並使用此方法在用戶點擊圖像小部件時禁用滾動行為。
我發現使用此方法的一個方便行為是用戶仍然可以根據需要向上或向下滾動(點擊並立即滑動而不是點擊向下然后滑動)。 這可能不是最好的方法,因為我不能只阻止向上滾動行為。
這是我的例子:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ScrollPhysics physics = const AlwaysScrollableScrollPhysics();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(
// Add the scroll physics controller here
physics: physics,
children: [
for (int i = 0; i < 20; i++) ...[
// Wrap the Widget with GestureDetector
GestureDetector(
// Disable the scroll behavior when users tap down
onTapDown: (_) {
setState(() {
physics = const NeverScrollableScrollPhysics();
});
},
// Enable the scroll behavior when user leave
onTapCancel: () {
setState(() {
physics = const AlwaysScrollableScrollPhysics();
});
},
onPanUpdate: (details) {
// Catch the swipe up action.
if (details.delta.dy < 0) {
print('Swiping up the element $i');
}
// Catch the swipe down action.
if (details.delta.dy > 0) {
print('Swiping down the element $i');
}
},
// Your image widget here
child: Padding(
padding: const EdgeInsets.all(20),
child: Container(
width: 100,
height: 100,
color: Colors.red,
),
),
),
Center(child: Text('Element $i')),
],
],
),
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.