Here’s an example of a basic shopping cart app in Flutter:

import ‘package:flutter/material.dart’;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Shopping Cart’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: ShoppingCart(),
);
}
}

class ShoppingCart extends StatefulWidget {
@override
_ShoppingCartState createState() => _ShoppingCartState();
}

class _ShoppingCartState extends State<ShoppingCart> {
List<String> _products = [];

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘Shopping Cart’),
),
body: ListView.builder(
itemCount: _products.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_products[index]),
trailing: IconButton(
icon: Icon(Icons.delete),
onPressed: () {
setState(() {
_products.removeAt(index);
});
},
),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(‘Add product’),
content: TextField(
onSubmitted: (value) {
setState(() {
_products.add(value);
});
Navigator.of(context).pop();
},
),
actions: <Widget>[
FlatButton(
child: Text(‘Cancel’),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
},
child: Icon(Icons.add),
),
);
}
}

 

 

 

 

In this example, a ShoppingCart widget is created which extends StatefulWidget. The state of the widget is managed by _ShoppingCartState. The _products list stores the items in the shopping cart. The UI consists of a list of the items in the cart and a floating action button to add new items to the cart. When the floating action button is pressed, a dialog pops up to allow the user to enter the name of a new product, which is then added to the _products list. The list of products is displayed using a ListView.builder. Each item in the list has a delete icon, which removes the item from the _products list when pressed.

Facebook Comments Box
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments