A Flutter package to create Add To Cart Button. It will be useful for your awesome app.
This creates a button that is used to let the users can add items to their shopping cart. This will be useful for the eCommerce app.
import 'package:flutter/material.dart';
import 'package:flutter_add_to_cart_button/flutter_add_to_cart_button.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
AddToCartButtonStateId stateId = AddToCartButtonStateId.idle;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Add To Cart'),
),
body: SingleChildScrollView(
child: Column(
children: [
Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: AddToCartButton(
trolley: Image.asset(
'assets/ic_cart.png',
width: 24,
height: 24,
color: Colors.white,
),
text: Text(
'Add to cart',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
),
maxLines: 1,
overflow: TextOverflow.fade,
),
check: SizedBox(
width: 48,
height: 48,
child: Icon(
Icons.check,
color: Colors.white,
size: 24,
),
),
borderRadius: BorderRadius.circular(24),
backgroundColor: Colors.deepOrangeAccent,
onPressed: (id) {
if (id == AddToCartButtonStateId.idle) {
//handle logic when pressed on idle state button.
setState(() {
stateId = AddToCartButtonStateId.loading;
Future.delayed(Duration(seconds: 3), () {
setState(() {
stateId = AddToCartButtonStateId.done;
});
});
});
} else if (id == AddToCartButtonStateId.done) {
//handle logic when pressed on done state button.
setState(() {
stateId = AddToCartButtonStateId.idle;
});
}
},
stateId: stateId,
),
),
),
],
),
),
),
);
}
}
[!] Flutter (Channel stable, 3.24.3, on Microsoft Windows [Version 10.0.19045.5131], locale en-US)
• Flutter version 3.24.3 on channel stable at C:\Users\admin\fvm\default
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 2663184aa7 (3 months ago), 2024-09-11 16:27:48 -0500
• Engine revision 36335019a8
• Dart version 3.5.3
• DevTools version 2.37.3