1. **Add Dependencies:**
Open your Flutter project's `pubspec.yaml` file and add the necessary dependency for icons. You can use the `flutter_icons` package to easily include icons.
```yaml
dependencies:
flutter:
sdk: flutter
flutter_icons: ^1.1.0
```
Run `flutter pub get` to install the new dependency.
2. **Create a Custom AppBar:**
In your app's main widget, you can create a custom `AppBar` that includes the shopping cart icon with the item count.
```dart
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart'; // Import the Flutter Icons library
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Shopping Cart Icon',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Shopping Cart'),
actions: [
ShoppingCartIcon(
itemCount: 3, // Pass your actual item count here
),
SizedBox(width: 20), // Add some spacing between icons
],
),
body: Center(
child: Text('Your app content goes here.'),
),
);
}
}
class ShoppingCartIcon extends StatelessWidget {
final int itemCount;
const ShoppingCartIcon({required this.itemCount});
@override
Widget build(BuildContext context) {
return Stack(
children: [
IconButton(
onPressed: () {
// Handle shopping cart click
},
icon: Icon(FontAwesome.shopping_cart),
),
Positioned(
top: 5,
right: 5,
child: Container(
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(10),
),
constraints: BoxConstraints(
minWidth: 16,
minHeight: 16,
),
child: Text(
itemCount.toString(),
style: TextStyle(
color: Colors.white,
fontSize: 10,
),
textAlign: TextAlign.center,
),
),
),
],
);
}
}
```
3. **Replace `itemCount` with Actual Count:**
In the `HomePage` widget, replace the `itemCount` value with the actual count of items in the shopping cart.
Now, when you run your app, you should see a custom `AppBar` with a shopping cart icon and the item count displayed as a badge. You can adjust the positioning, colors, and styles as needed to match your app's design.