Last active
November 16, 2020 20:25
-
-
Save samuelematias/45d6431a3ae78dcbfa461a522dde1803 to your computer and use it in GitHub Desktop.
A simple Carousel widget without Transition for Flutter apps.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class Carousel<T> extends StatelessWidget { | |
final double height; | |
final List<T> items; | |
final Widget child; | |
final double itemExtent; | |
final double spaceBetweenItems; | |
final double spacingFirstItem; | |
final double spacingLastItem; | |
const Carousel({ | |
Key key, | |
this.height = 150, | |
@required this.items, | |
@required this.child, | |
this.itemExtent, | |
this.spaceBetweenItems = 16.0, | |
this.spacingFirstItem = 16.0, | |
this.spacingLastItem = 16.0, | |
}) : assert(items != null), | |
assert(child != null), | |
super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
height: height, | |
child: ListView.builder( | |
scrollDirection: Axis.horizontal, | |
itemCount: items.length, | |
itemExtent: itemExtent, | |
itemBuilder: (_, int index) { | |
final bool isFirstItem = index == 0; | |
final bool isLastItem = index + 1 == items.length; | |
return Padding( | |
padding: EdgeInsets.only( | |
left: isFirstItem ? spacingFirstItem : 0.0, | |
right: isLastItem ? spacingLastItem : spaceBetweenItems, | |
), | |
child: child, | |
); | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment