简体   繁体   中英

Flutter:OnTap not working when Listview is scrolling

I tried to create a marquee Widget, I using the Listview implement auto-scrolling, but if I added a click event to the text when listview is scrolling, it not clickable.I know it should be a rolling problem, but I don't know how to solve itHere's the code is Marquee widget.

  import 'dart:async';
    import 'package:flutter/material.dart';
    import 'package:flutter/widgets.dart';

    class MarqueeContinuous extends StatefulWidget {
      final Widget child;
      final Duration duration;
      final double stepOffset;

      MarqueeContinuous(
          {Key key,
          this.child,
          this.duration = const Duration(seconds: 3),
          this.stepOffset = 50.0})
          : super(key: key);

      @override
      _MarqueeContinuousState createState() => _MarqueeContinuousState();
    }

    class _MarqueeContinuousState extends State<MarqueeContinuous> {
      ScrollController _controller;
      Timer _timer;
      double _offset = 0.0;

      @override
      void initState() {
        super.initState();
        _controller = ScrollController(initialScrollOffset: _offset);
        _timer = Timer.periodic(widget.duration, (timer) {
          double newOffset = _controller.offset + widget.stepOffset;

          if (newOffset != _offset) {
            _offset = newOffset;
            _controller.animateTo(_offset,
                duration: widget.duration, curve: Curves.linear);
          }
        });
      }

      @override
      void dispose() {
        _timer.cancel();
        _controller.dispose();
        super.dispose();
      }

      @override
      Widget build(BuildContext context) {
        return ListView.builder(
                    physics: NeverScrollableScrollPhysics(),
                    scrollDirection: Axis.horizontal,
                    controller: _controller,
                    addAutomaticKeepAlives: false,
                    itemBuilder: (context, index) {
                      return widget.child;
                    });
      }
    }

ListView或任何要向其添加点击小部件的Scrollable小部件移除shrinkWrapNeverScrollableScrollPhysics

Issue persists in flutter even in marquee package on tap doesn't work as intended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM