Code Monkey home page Code Monkey logo

geekyants / infinite-carousel-flutter Goto Github PK

View Code? Open in Web Editor NEW
43.0 8.0 26.0 22.61 MB

Carousel in flutter. Supports features like infinite looping, friction effect, multiple scroll physics and control over item anchor and velocity.

Home Page: https://pub.dev/packages/infinite_carousel

License: MIT License

Kotlin 0.14% Swift 2.01% Objective-C 0.04% Dart 43.91% HTML 2.14% CMake 22.53% C++ 27.57% C 1.65%
flutter carousel infinite-scroll widget geekyants

infinite-carousel-flutter's Introduction

Infinite Carousel

Version pub points Popularity Pub Likes GitHub issues by-label

Infinite Carousel helps you to create interactive carousel experience within your Flutter applications.

Features

Infinite Carousel does not use PageView underneath, therefore it has some unique features not present in PageView based Carousels like:

Granular control over selected item placement

It allows you to control placement (or anchor) of selected item on viewport (unlike PageView which always has its selected page in the center of viewport).

Fine tuning Scroll Velocity

It gives control over of scrolling speed, therefore allowing user to increase / slow the carousel movements when scrolled free hand.

Friction effect

It simulates a realistic slowing down effect after scrolling, adding a touch of natural behavior and refinement.

Supports Multiple Scroll Physics

Default behaviour is InfiniteScrollPhysics, which makes sure we always land on an item after free hand scrolling, with added friction effect. But you can also set it to PageViewTypeScrollPhysics to get the PageView like scroll effect.

Infinite looping

It supports infinite looping on items, and exposes both actual indices and relative indices of items.

Horizontal and Vertical Axis

It supports both horizontal and vertical axis directions with respect to LTR and RTL text directions.

Usage

Add the package to pubspec.yaml

$ flutter pub add infinite_carousel

or

dependencies:
  infinite_carousel: ^1.1.1

After that import the package.

import 'package:infinite_carousel/infinite_carousel.dart';
InfiniteCarousel.builder(
  itemCount: 10,
  itemExtent: 120,
  center: true,
  anchor: 0.0,
  velocityFactor: 0.2,
  onIndexChanged: (index) {},
  controller: controller,
  axisDirection: Axis.horizontal,
  loop: true,
  itemBuilder: (context, itemIndex, realIndex) {
  },
)

Controller

You can programatically control the Carousel movement by providing InfiniteScrollController, which provides number of useful methods.

  • pass initialItem in the constructor.
  • animateToItem(index)
  • jumpToItem(index)
  • nextItem()
  • previousItem()

Attributes

Attribute Data Type Description Default Value
itemCount int Total number of items. Required
itemExtent double Size of single item in viewport. If axis is horizontal, then this corresponds to width of item, else height of the item. Required
itemBuilder Widget Function(BuildContext, int itemIndex, int realIndex) To lazily build items on the viewport. When Loop is false, ItemIndex is equal to RealIndex (i.e, index of element). When loop is true, two indexes are exposed by item builder. One is itemIndex, that is the modded item index i.e., for list of 10, position(11) = 1, and position(-1) = 9. Other is realIndex, that is the actual index, i.e. (..., -2, -1, 0, 1, 2, ...) in loop. Real Index is needed if you want to support JumpToItem by tapping on it. Required
physics ScrollPhysics Defaults to InfiniteScrollPhysics, which makes sure we always land on a particular item after free-hand scrolling. You can also provide PageViewTypeScrollPhysics to get PageView scroll effect. InfiniteScrollPhysics
controller ScrollController Scroll Controller for the carousel. InfiniteScrollController
onIndexChanged void Function(int) Callback that includes new selected index.
anchor double Where to place selected item on the viewport. Ranges from 0 to 1. 0.0 means selected item is aligned to start of the viewport, and 1.0 meaning selected item is aligned to end of the viewport. This property is ignored when center is set to true. 0.0
loop bool Whether to create a infinite looping list true
velocityFactor double Increase/Decrease velocity of carousel scrolling to this factor. 0.2
axisDirection Axis Axis direction of carousel. Axis.horizontal
center bool Align selected item to center of the viewport. When this is true, anchor property is ignored. true

Flutter web

Earlier, flutter allowed scrolling the widgets by dragging using the mouse. Now, by default, it allows scrolling widgets to be dragged by all PointerDeviceKinds except for PointerDeviceKind.mouse.

If you want to support scrolling by drag, please pass the explicit ScrollBehavior to InfiniteCarousel.

scrollBehavior: ScrollConfiguration.of(context).copyWith(
  dragDevices: {
    // Allows to swipe in web browsers
    PointerDeviceKind.touch,
    PointerDeviceKind.mouse
  },
),

Reference: https://docs.flutter.dev/release/breaking-changes/default-scroll-behavior-drag

Contributing

Contributions of all kinds are welcome. Please read CONTRIBUTING.md and CODE_OF_CONDUCT.md before raising issues and creating new pull requests.

Images used in the gifs belong to their original authors and the links can be found in example project.

Contributors


Manuinder Sekhon @ GeekyAnts (Github)

infinite-carousel-flutter's People

Contributors

al4nnw avatar fretelweb avatar j-j-gajjar avatar macjohson avatar manuindersekhon avatar maple135790 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

infinite-carousel-flutter's Issues

Add autoplay option

Please is possible add a option for the carousel running alone ? Like autoplay

Always land on next item

Is there a way to always stop in the next item? Even if the person scrolls really fast? Like a "page view"?

Display only one item

is there a way to display only one item? In my case I have multiple images and i want it to scroll but then it snaps to the center and no other image is displayed.

'FixedScrollMetrics' is not a subtype of type 'InfiniteExtentMetrics' in type cast

Hi! I using some vertical scrolable widget inside InfiniteCarousel and that works, but with some exception, how can i fix it?

════════ Exception caught by gesture ═══════════════════════════════════════════
The following _TypeError was thrown while handling a gesture:
type 'FixedScrollMetrics' is not a subtype of type 'InfiniteExtentMetrics' in type cast

When the exception was thrown, this was the stack
#0 _InfiniteCarouselState.build. infinite_carousel.dart:163
#1 _NotificationElement.onNotification notification_listener.dart:130
#2 _NotificationNode.dispatchNotification framework.dart:3197
#3 _NotificationNode.dispatchNotification framework.dart:3200
#4 _NotificationNode.dispatchNotification framework.dart:3200
#5 _NotificationNode.dispatchNotification framework.dart:3200
#6 _NotificationNode.dispatchNotification
#7 _NotificationNode.dispatchNotification framework.dart:3200
#8 _NotificationNode.dispatchNotification framework.dart:3200
#9 Element.dispatchNotification framework.dart:4551
#10 Notification.dispatch notification_listener.dart:60
#11 DragScrollActivity.dispatchScrollUpdateNotification scroll_activity.dart:465
#12 ScrollPosition.didUpdateScrollPositionBy scroll_position.dart:896
#13 ScrollPosition.setPixels scroll_position.dart:283
#14 ScrollPositionWithSingleContext.setPixels scroll_position_with_single_context.dart:78
#15 ScrollPositionWithSingleContext.applyUserOffset scroll_position_with_single_context.dart:122
#16 ScrollDragController.update scroll_activity.dart:385
#17 ScrollableState._handleDragUpdate
#18 DragGestureRecognizer._checkUpdate. monodrag.dart:516
#19 GestureRecognizer.invokeCallback recognizer.dart:275
#20 DragGestureRecognizer._checkUpdate monodrag.dart:516
#21 DragGestureRecognizer.handleEvent monodrag.dart:370
#22 PointerRouter._dispatch pointer_router.dart:98
#23 PointerRouter._dispatchEventToRoutes. pointer_router.dart:143
#24 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:625:13)
#25 PointerRouter._dispatchEventToRoutes pointer_router.dart:141
#26 PointerRouter.route pointer_router.dart:127
#27 GestureBinding.handleEvent binding.dart:465
#28 GestureBinding.dispatchEvent binding.dart:445
#29 RendererBinding.dispatchEvent binding.dart:331
#30 GestureBinding._handlePointerEventImmediately binding.dart:400
#31 GestureBinding.handlePointerEvent binding.dart:363
#32 GestureBinding._flushPointerEventQueue binding.dart:320
#33 GestureBinding._handlePointerDataPacket binding.dart:293
#34 _invoke1 (dart:ui/hooks.dart:158:13)
#35 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:382:7)
#36 _dispatchPointerDataPacket (dart:ui/hooks.dart:91:31)
Handler: "onUpdate"
Recognizer: VerticalDragGestureRecognizer#c741f
start behavior: start

The carousel don't detect the mouse for scrolling on the web

Hello,
I'm trying to use this package for a web page. The carousel doesn't scroll on the desktop browser but it do it right in the cellphone browser.
I experienced this issue in other scrolls (like the page view) and the problem was that the scroll was not recognizing the mouse.
The solution in that case was to set the scroll behavior like follows (but I couldn't find a parameter to set it on this widget):

scrollBehavior: ScrollConfiguration.of(context).copyWith(
            dragDevices: {
              // Allows to swipe in web browsers
              PointerDeviceKind.touch,
              PointerDeviceKind.mouse
            },
          ),

The code I have right now is the following:

return Container(
     height: _cardHeight * 1.5,
     child: InfiniteCarousel.builder(
       itemCount: widget.assetsList.length,
       itemExtent: _cardWidth,
       center: true,
       anchor: 0.0,
       velocityFactor: 0.2,
       onIndexChanged: (index) {},

       // controller: controller,
       axisDirection: Axis.horizontal,
       loop: true,
       itemBuilder: (context, itemIndex, realIndex) {
         return Container(
           alignment: Alignment.center,
           child: GestureDetector(
             behavior: HitTestBehavior.opaque,
             child: Image.asset(
               '${LandingConstants.carouselImagesPath}${widget.assetsList[itemIndex]}',
               width: _cardWidth,
             ),
           ),
         );
       },
     ),
   );

automatic height

Why do I need to put the widget in a container with a fixed height?
What should I do if I need automatic height?

Thanks <3

Question about item jump

Example:

Number of projects: 10

Mode: Infinite Loop

ItemBuilder: (context, itemIndex, realIndex){

After scrolling left, the latest realIndex=-5, itemIndex=1

_ Controller. animateToItem (-4) corresponds to itemIndex=2

}

RealIndex=-3, itemIndex=3

Click the button to jump to controller. animateToItem (3)

The scrolling method is strange, I understand, but I need to change this situation

For example, the current realIndex=-42 itemIndex=5

Controller. animateToItem (3) realIndex=-40 instead of 3

Do you have any ideas

[Question] Scaling Selected Item

The first GIF on the pub.dev page shows the selected item scale animation as you scroll. Is that functionality part of this package or just done using other widgets?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.