2

I am building a three-row prompt slider in Flutter that needs to scroll both automatically and manually. I'm having trouble implementing the infinite scrolling behavior.

animation like

I tried using PageView with a custom controller, but I couldn't get the infinite loop effect. I also tried using AnimationController, but the animation wasn't smooth

3
  • 1
    post your PageView code then, how did you use its PageController? Commented Mar 11 at 10:03
  • Tried and removed, but its not looking this way. Can you please help me with any other solution Commented Mar 11 at 10:09
  • Try to use pub.dev/packages/marquee package. Commented Mar 11 at 10:19

1 Answer 1

2

Try this code I tried to implement it with ListView this is the code:

class SmoothInfiniteSlider extends StatefulWidget {
  const SmoothInfiniteSlider({super.key});

  @override
  State<SmoothInfiniteSlider> createState() => _SmoothInfiniteSliderState();
}

class _SmoothInfiniteSliderState extends State<SmoothInfiniteSlider> {
  final ScrollController _scrollController = ScrollController();
  Timer? _autoScrollTimer;
  bool _isUserInteracting = false;

  List<String> items = [
    "🎃 History of Halloween?",
    "🍊 Tangerine vs Orange",
    "⚙️ Tech Trends",
    "🐝 Why do we only see one side of the moon?",
    "💰 How to save money?",
    "🚀 Space Facts",
    "🌍 The world",
    "🤺 Fencing practice set price",
    "🐍 What is Python?",
  ];

  double _scrollSpeed = 1.0; // Adjust speed

  @override
  void initState() {
    super.initState();
    _startAutoScroll();
  }

  void _startAutoScroll() {
    _autoScrollTimer?.cancel();

    _autoScrollTimer = Timer.periodic(Duration(milliseconds: 30), (timer) {
      if (_scrollController.hasClients && !_isUserInteracting) {
        _scrollController.jumpTo(_scrollController.offset + _scrollSpeed);

        if (_scrollController.offset >=
            _scrollController.position.maxScrollExtent - 200) {
          _scrollController.jumpTo(0);
        }
      }
    });
  }

  @override
  void dispose() {
    _autoScrollTimer?.cancel();
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // Duplicate the list to create an infinite scrolling effect
    List<String> infiniteItems = [...items, ...items];

    return Scaffold(
      backgroundColor: Colors.white,
      body: GestureDetector(
        onPanDown: (_) {
          _isUserInteracting = true;
        },
        onPanCancel: () {
          _isUserInteracting = false;
          _startAutoScroll();
        },
        onPanEnd: (_) {
          _isUserInteracting = false;
          _startAutoScroll();
        },
        child: ListView.builder(
          controller: _scrollController,
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(), // Allow manual scroll
          itemCount: infiniteItems.length,
          itemBuilder: (context, index) {
            final text = infiniteItems[index];
            return Padding(
              padding: EdgeInsets.symmetric(horizontal: 10),
              child: Center(child: Text(text, style: TextStyle(fontSize: 16))),
            );
          },
        ),
      ),
    );
  }
}

which is not exactly the the way you wanted but you can get the idea. I also implemented it with gridview which was will be somewhat near to what you need if you implemented it StaggeredGridView which allow different size of children. or you can use marquee package for this functionality.

Sign up to request clarification or add additional context in comments.

4 Comments

It should allow manual scroll too
check the updated answer.
Thank you. for multiline is there any idea ?
use grid but if you use simple grid that might not work perfectly as the child sizes are same in the simple grid if you use StaggeredGridView package you might get the intended results. and also give a try to marquee packages also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.