gpt4 book ai didi

flutter - 有没有办法阻止手势在 flutter 中冒泡?

转载 作者:行者123 更新时间:2023-12-03 16:26:22 25 4
gpt4 key购买 nike

我有 3 个 child 的 TabBarView,其中一个我有一个容器,其中有一个 onPanUpdate手势。

当我尝试移动容器标签栏时 onHorizontalDragUpdate调用和选项卡栏更改选项卡但不更改容器。

如果我在 javascript 中像 stoppropagation 这样的容器上贴上胶带,有什么方法可以防止 onHorizo​​ntalDragUpdate 吗?

TabBarView(
controller: _tabController,
children: [
Container(
color: Colors.red,
),
TabWithDragableContainer(),
Container(
color: Colors.blue,
),
],
)

最佳答案

您可以通过指定 physics: NeverScrollableScrollPhysics() 来禁用所有选项卡的手势。在 TabBarView 里面。
最小工作示例
enter image description here
此示例具有三个选项卡:

  • 基础版 Tab
  • 带有 Draggable 的标签小工具
  • 带有 onPan 的标签 GestureDetector

  • import 'package:flutter/material.dart';
    import 'package:flutter_hooks/flutter_hooks.dart';

    void main() {
    runApp(
    MaterialApp(
    debugShowCheckedModeBanner: false,
    title: 'Flutter Demo',
    home: HomePage(),
    ),
    );
    }

    class HomePage extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    return DefaultTabController(
    length: 3,
    child: Scaffold(
    appBar: AppBar(
    bottom: TabBar(
    tabs: [
    Tab(icon: Icon(Icons.directions_car)),
    Tab(icon: Icon(Icons.directions_transit)),
    Tab(icon: Icon(Icons.directions_bike)),
    ],
    ),
    ),
    body: TabBarView(
    physics: NeverScrollableScrollPhysics(),
    children: [
    Icon(Icons.directions_car),
    _ContainerWithDraggable(),
    _ContainerWithPanGesture(),
    ],
    ),
    ),
    );
    }
    }

    class _ContainerWithDraggable extends HookWidget {
    @override
    Widget build(BuildContext context) {
    final _offset = useState(Offset(100, 100));
    return Stack(
    children: [
    Container(color: Colors.amber.shade100),
    Positioned(
    top: _offset.value.dy,
    left: _offset.value.dx,
    child: Draggable(
    onDragUpdate: (details) => _offset.value += details.delta,
    feedback: _Circle(),
    child: _Circle(),
    ),
    ),
    ],
    );
    }
    }

    class _ContainerWithPanGesture extends HookWidget {
    @override
    Widget build(BuildContext context) {
    final _offset = useState(Offset(100, 100));
    final _previousOffset = useState<Offset>(null);
    final _referenceOffset = useState<Offset>(null);
    return Stack(
    children: [
    GestureDetector(
    onPanStart: (details) {
    _previousOffset.value = _offset.value;
    _referenceOffset.value = details.localPosition;
    },
    onPanUpdate: (details) => _offset.value = _previousOffset.value +
    details.localPosition -
    _referenceOffset.value,
    child: Container(color: Colors.amber.shade100),
    ),
    Positioned(
    top: _offset.value.dy,
    left: _offset.value.dx,
    child: _Circle(),
    ),
    ],
    );
    }
    }

    class _Circle extends StatelessWidget {
    const _Circle({
    Key key,
    }) : super(key: key);

    @override
    Widget build(BuildContext context) {
    return Container(
    width: 50,
    height: 50,
    decoration: BoxDecoration(
    shape: BoxShape.circle,
    border: Border.all(color: Colors.brown, width: 5.0),
    ),
    );
    }
    }

    关于flutter - 有没有办法阻止手势在 flutter 中冒泡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58583282/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com