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

Share:
192
luckysmg
Author by

luckysmg

Updated on December 31, 2022

Comments

  • luckysmg
    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.

    enter image description here

    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
      Yeasin Sheikh almost 3 years
      do you like to change the page
    • Yeasin Sheikh
      Yeasin Sheikh almost 3 years
      and you can use physics: NeverScrollableScrollPhysics(), at PageViewBuilder for not to scroll.
    • luckysmg
      luckysmg almost 3 years
      But I need this PageView scroll when my finger is in PageView but not in that widget @YeasinSheikh
    • Yeasin Sheikh
      Yeasin Sheikh almost 3 years
      lets'say you want gesture detector for both page and widget change on horizontal scroll ?
    • luckysmg
      luckysmg almost 3 years
      I have a solution,I use RawGestureDectector,and use Horizontal and Vertical gestures
    • luckysmg
      luckysmg over 2 years
      Thx,It can work,but a little bit complex😀