Want to add horizontally scrollable cards with Snap effect in flutter? Use PageView and ListView to add horizontally scrollable cards with Snap effect.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import 'package:flutter/material.dart'; main() => runApp(MaterialApp(home: MyHomePage())); class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Carousel in vertical scrollable'), ), body: ListView.builder( padding: EdgeInsets.symmetric(vertical: 16.0), itemBuilder: (BuildContext context, int index) { if(index % 2 == 0) { return _buildCarousel(context, index ~/ 2); } else { return Divider(); } }, ), ); } Widget _buildCarousel(BuildContext context, int carouselIndex) { return Column( mainAxisSize: MainAxisSize.min, children: <Widget>[ Text('Carousel $carouselIndex'), SizedBox( // you may want to use an aspect ratio here for tablet support height: 200.0, child: PageView.builder( // store this controller in a State to save the carousel scroll position controller: PageController(viewportFraction: 0.8), itemBuilder: (BuildContext context, int itemIndex) { return _buildCarouselItem(context, carouselIndex, itemIndex); }, ), ) ], ); } Widget _buildCarouselItem(BuildContext context, int carouselIndex, int itemIndex) { return Padding( padding: EdgeInsets.symmetric(horizontal: 4.0), child: Container( decoration: BoxDecoration( color: Colors.grey, borderRadius: BorderRadius.all(Radius.circular(4.0)), ), ), ); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.