Tutorial and code of Scrollable Cards in Flutter. Copy and paste the below code as per your requirements.
import 'package:flutter/material.dart';
const MyData = [
{
"name":"This is content 1",
"brand":"Subcontent 1",
"price":100,
},{
"name":"This is content 2",
"brand":"Subcontent 2",
"price":100,
},
{
"name":"This is content 3",
"brand":"Subcontent 3",
"price":100,
},
{
"name":"This is content 4",
"brand":"Subcontent 4",
"price":100,
},
{
"name":"This is content 5",
"brand":"Subcontent 5",
"price":100,
},
{
"name":"This is content 6",
"brand":"Subcontent 6",
"price":100,
},
{
"name":"This is content 7",
"brand":"Subcontent 7",
"price":100,
"image":"pizza.png"
},
{
"name":"This is content 8",
"brand":"Subcontent 8",
"price":100,
},
{
"name":"This is content 9",
"brand":"Subcontent 9",
"price":100,
}
];
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final CategoriesScroller categoriesScroller = const CategoriesScroller();
ScrollController controller = ScrollController();
bool closeTopContainer = false;
double topContainer = 0;
List<Widget> itemsData = [];
void getPostsData() {
List<dynamic> responseList = MyData;
List<Widget> listItems = [];
for (var post in responseList) {
listItems.add(Container(
height: 150,
margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
decoration: BoxDecoration(borderRadius: const BorderRadius.all(Radius.circular(20.0)), color: Colors.white, boxShadow: [
BoxShadow(color: Colors.black.withAlpha(100), blurRadius: 10.0),
]),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
post["name"],
style: const TextStyle(fontSize: 28, fontWeight: FontWeight.bold),
),
Text(
post["brand"],
style: const TextStyle(fontSize: 17, color: Colors.grey),
),
const SizedBox(
height: 10,
),
Text(
"\$ ${post["price"]}",
style: const TextStyle(fontSize: 25, color: Colors.black, fontWeight: FontWeight.bold),
)
],
),
],
),
)));
}
setState(() {
itemsData = listItems;
});
}
@override
void initState() {
super.initState();
getPostsData();
controller.addListener(() {
double value = controller.offset/119;
setState(() {
topContainer = value;
closeTopContainer = controller.offset > 50;
});
});
}
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
final double categoryHeight = size.height*0.30;
return SafeArea(
child: Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.white,
leading: const Icon(
Icons.menu,
color: Colors.black,
),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.search, color: Colors.black),
onPressed: () {},
),
IconButton(
icon: const Icon(Icons.person, color: Colors.black),
onPressed: () {},
)
],
),
body: SizedBox(
height: size.height,
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: const <Widget>[
Text(
"Loyality Cards",
style: TextStyle(color: Colors.grey, fontWeight: FontWeight.bold, fontSize: 20),
),
Text(
"Menu",
style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 20),
),
],
),
const SizedBox(
height: 10,
),
AnimatedOpacity(
duration: const Duration(milliseconds: 200),
opacity: closeTopContainer?0:1,
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
width: size.width,
alignment: Alignment.topCenter,
height: closeTopContainer?0:categoryHeight,
child: categoriesScroller),
),
Expanded(
child: ListView.builder(
controller: controller,
itemCount: itemsData.length,
physics: const BouncingScrollPhysics(),
itemBuilder: (context, index) {
double scale = 1.0;
if (topContainer > 0.5) {
scale = index + 0.5 - topContainer;
if (scale < 0) {
scale = 0;
} else if (scale > 1) {
scale = 1;
}
}
return Opacity(
opacity: scale,
child: Transform(
transform: Matrix4.identity()..scale(scale,scale),
alignment: Alignment.bottomCenter,
child: Align(
heightFactor: 0.7,
alignment: Alignment.topCenter,
child: itemsData[index]),
),
);
})),
],
),
),
),
);
}
}
class CategoriesScroller extends StatelessWidget {
const CategoriesScroller({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final double categoryHeight = MediaQuery.of(context).size.height * 0.30 - 50;
return SingleChildScrollView(
physics: const BouncingScrollPhysics(),
scrollDirection: Axis.horizontal,
child: Container(
margin: const EdgeInsets.symmetric(vertical: 20, horizontal: 20),
child: FittedBox(
fit: BoxFit.fill,
alignment: Alignment.topCenter,
child: Row(
children: <Widget>[
Container(
width: 150,
margin: const EdgeInsets.only(right: 20),
height: categoryHeight,
decoration: const BoxDecoration(color: Colors.indigo, borderRadius: BorderRadius.all(Radius.circular(20.0))),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const <Widget>[
Text(
"Most\nFavorites",
style: TextStyle(fontSize: 25, color: Colors.white, fontWeight: FontWeight.bold),
),
SizedBox(
height: 10,
),
Text(
"20 Items",
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
),
),
),
Container(
width: 150,
margin: const EdgeInsets.only(right: 20),
height: categoryHeight,
decoration: const BoxDecoration(color: Colors.redAccent, borderRadius: BorderRadius.all(Radius.circular(20.0))),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const <Widget>[
Text(
"Newest",
style: TextStyle(fontSize: 25, color: Colors.white, fontWeight: FontWeight.bold),
),
SizedBox(
height: 10,
),
Text(
"20 Items",
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
),
),
),
Container(
width: 150,
margin: const EdgeInsets.only(right: 20),
height: categoryHeight,
decoration: const BoxDecoration(color: Colors.green, borderRadius: BorderRadius.all(Radius.circular(20.0))),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const <Widget>[
Text(
"Super\nSaving",
style: TextStyle(fontSize: 25, color: Colors.white, fontWeight: FontWeight.bold),
),
SizedBox(
height: 10,
),
Text(
"20 Items",
style: TextStyle(fontSize: 16, color: Colors.white),
),
],
),
),
),
],
),
),
),
);
}
}