In flutter How to make the inner widget with pan gesture will not conflict with PageView
192
Use RawGestureDectector,and use Horizontal and Vertical gestures to avoid this solution
Author by
luckysmg
Updated on December 31, 2022Comments
-
luckysmg over 1 year
I have a widget with pan gesture in PageView ,and the PageView can scoll horizontally,When I pan the widget horizontally,the PageView will move.
What I expect is that the pan gesture can move the widget in the pageview,instead of moving the PageView
Here is the picture.
Here is the demo code"
import 'package:flutter/material.dart'; class HomePage extends StatefulWidget { const HomePage({Key? key}) : super(key: key); @override _HomePageState createState() => _HomePageState(); } class _HomePageState extends State<HomePage> { @override Widget build(BuildContext context) { return Scaffold( body: PageView.builder( itemBuilder: (ctx, index) { return ChildPage( index: index, ); }, itemCount: 2, ), ); } } class ChildPage extends StatefulWidget { final int index; const ChildPage({Key? key, required this.index}) : super(key: key); @override _ChildPageState createState() => _ChildPageState(); } class _ChildPageState extends State<ChildPage> { Offset offset = Offset.zero; @override Widget build(BuildContext context) { return Scaffold( body: Stack( alignment: Alignment.center, children: [ Positioned( top: offset.dy, left: offset.dx, child: GestureDetector( onPanUpdate: (details) { print("hh"); offset = offset.translate(details.delta.dx, details.delta.dy); setState(() {}); }, child: Container( color: Colors.yellow, width: 100, height: 100, ), )) ], ), ); } }
-
Yeasin Sheikh almost 3 yearsdo you like to change the page
-
Yeasin Sheikh almost 3 yearsand you can use
physics: NeverScrollableScrollPhysics(),
atPageViewBuilder
for not to scroll. -
luckysmg almost 3 yearsBut I need this PageView scroll when my finger is in PageView but not in that widget @YeasinSheikh
-
Yeasin Sheikh almost 3 yearslets'say you want gesture detector for both page and widget change on horizontal scroll ?
-
luckysmg almost 3 yearsI have a solution,I use RawGestureDectector,and use Horizontal and Vertical gestures
-
luckysmg over 2 yearsThx,It can work,but a little bit complex😀
-