【问题标题】:In flutter How to make the inner widget with pan gesture will not conflict with PageView在颤振中如何使带有平移手势的内部小部件不会与PageView冲突
【发布时间】:2021-09-27 05:33:51
【问题描述】:

我在PageView中有一个带有平移手势的小部件,并且PageView可以水平滑动,当我水平平移小部件时,PageView会移动。

我期望的是平移手势可以移动页面视图中的小部件,而不是移动页面视图

这是图片。

这里是演示代码”

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,
                ),
              ))
        ],
      ),
    );
  }
}

【问题讨论】:

  • 你要换页吗
  • 您可以使用physics: NeverScrollableScrollPhysics(), PageViewBuilder 来避免滚动。
  • 但是当我的手指在 PageView 但不在那个小部件中时,我需要这个 PageView 滚动 @YeasinSheikh
  • 假设您希望在水平滚动时页面和小部件都发生变化的手势检测器?
  • 我有一个解决方案,我使用 RawGestureDectector,并使用水平和垂直手势

标签: android ios flutter flutter-layout flutter-pageview


【解决方案1】:

使用 RawGestureDectector,并使用水平和垂直手势来避免这种解决方案

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 2018-11-12
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2020-08-31
    相关资源
    最近更新 更多