Created
March 2, 2022 15:47
-
-
Save CoderNamedHendrick/ce1c209baa4654c9c0272d170e6a2ce9 to your computer and use it in GitHub Desktop.
Cart quantity feature
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'; | |
import 'package:provider/provider.dart'; | |
const Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith( | |
scaffoldBackgroundColor: darkBlue, | |
), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: ListView.builder( | |
itemCount: 5, | |
itemBuilder: (context, index) => ChangeNotifierProvider( | |
create: (_) => CartQuantityProvider(), | |
child: MyWidget(), | |
), | |
), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Consumer<CartQuantityProvider>( | |
builder: (context, provider, _) => ListTile( | |
leading: IconButton( | |
onPressed: provider.decreaseQuantity, | |
icon: const Icon(Icons.done), | |
), | |
trailing: IconButton( | |
onPressed: provider.increaseQuantity, | |
icon: const Icon(Icons.add), | |
), | |
title: Text( | |
'${provider.value}', | |
style: Theme.of(context).textTheme.headline6, | |
), | |
), | |
); | |
} | |
} | |
class CartQuantityProvider extends ChangeNotifier { | |
int value = 0; | |
void increaseQuantity() { | |
value += 1; | |
notifyListeners(); | |
} | |
void decreaseQuantity() { | |
value -= 1; | |
notifyListeners(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment