import
'package:add_to_cart_animation/add_to_cart_animation.dart'
;
import
'package:flutter/material.dart'
;
void
main() {
runApp(
const
MyApp());
}
class
MyApp extends StatelessWidget {
const
MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return
MaterialApp(
title:
'Shopping Cart Animation'
,
theme: ThemeData(
primarySwatch: Colors.green,
),
home:
const
ShoppingHomePage(),
debugShowCheckedModeBanner:
false
,
);
}
}
class
ShoppingHomePage extends StatefulWidget {
const
ShoppingHomePage({Key? key}) : super(key: key);
@override
_ShoppingHomePageState createState() => _ShoppingHomePageState();
}
class
_ShoppingHomePageState extends State<ShoppingHomePage> {
final GlobalKey<CartIconKey> cartKey = GlobalKey<CartIconKey>();
late Function(GlobalKey) runAddToCartAnimation;
var _cartQuantityItems = 0;
@override
Widget build(BuildContext context) {
return
AddToCartAnimation(
cartKey: cartKey,
height: 25,
width: 25,
opacity: 0.80,
dragAnimation:
const
DragToCartAnimationOptions(rotation:
true
),
createAddToCartAnimation: (runAddToCartAnimation) {
this
.runAddToCartAnimation = runAddToCartAnimation;
},
child: Scaffold(
appBar: AppBar(
title:
const
Text(
'Shopping Cart Animation'
),
centerTitle:
false
,
actions: [
IconButton(
icon:
const
Icon(Icons.cancel_presentation),
onPressed: clearCart,
),
const
SizedBox(width: 15),
AddToCartIcon(
key: cartKey,
icon:
const
Icon(Icons.shopping_cart),
badgeOptions:
const
BadgeOptions(
active:
true
,
backgroundColor: Colors.red,
),
),
const
SizedBox(width: 16),
],
),
body: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) => Column(
children: [
ShoppingListItem(onClick: listClick, index: index),
SizedBox(
height: 16),
],
),
),
),
);
}
void
clearCart() {
_cartQuantityItems = 0;
cartKey.currentState!.runClearCartAnimation();
}
void
listClick(GlobalKey widgetKey) async {
await runAddToCartAnimation(widgetKey);
await cartKey.currentState!
.runCartAnimation((++_cartQuantityItems).toString());
}
}
class
ShoppingListItem extends StatelessWidget {
final GlobalKey widgetKey = GlobalKey();
final
int
index;
final
void
Function(GlobalKey) onClick;
ShoppingListItem({required
this
.onClick, required
this
.index});
@override
Widget build(BuildContext context) {
List<String> shoppingImageUrls = [
];
String shoppingImageUrl =
shoppingImageUrls[index % shoppingImageUrls.length];
return
ListTile(
onTap: () => onClick(widgetKey),
leading: Container(
key: widgetKey,
width: 55,
height: 55,
color: Colors.transparent,
child: Image.network(
shoppingImageUrl,
width: 55,
height: 55,
),
),
title: Text(
"Shopping Item $index"
),
);
}
}