Flutter Implementation: Cart and Room Management
Flutter Shopping Cart Implementation
This example demonstrates how to create a simple Shopping Cart interface in Flutter using TextEditingController and StatefulWidget to manage item inputs and calculate totals.
import 'package:flutter/material.dart';
void main() { runApp(MyApp()); }
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: CartScreen());
}
}
class Item {
String name;
int qty;
double price;
Item(this.name, this.qty, this.price);
}
List<Item> cartList = [];
class CartScreen extends StatefulWidget {
@override
State<CartScreen> createState() => _CartScreenState();
}
class _CartScreenState extends State<CartScreen> {
TextEditingController name = TextEditingController();
TextEditingController qty = TextEditingController();
TextEditingController price = TextEditingController();
String message = "";
double total = 0;
void addItem() {
if (name.text.isEmpty || qty.text.isEmpty || price.text.isEmpty) {
setState(() { message = "All fields are required"; });
return;
}
int q = int.tryParse(qty.text) ?? 0;
double p = double.tryParse(price.text) ?? 0;
if (q <= 0 || p <= 0) {
setState(() { message = "Enter valid quantity and price"; });
return;
}
cartList.add(Item(name.text, q, p));
total += q * p;
name.clear();
qty.clear();
price.clear();
setState(() { message = "Item Added Successfully"; });
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Shopping Cart")),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
TextField(controller: name, decoration: InputDecoration(labelText: "Item Name")),
TextField(controller: qty, keyboardType: TextInputType.number, decoration: InputDecoration(labelText: "Quantity")),
TextField(controller: price, keyboardType: TextInputType.number, decoration: InputDecoration(labelText: "Price")),
SizedBox(height: 20),
ElevatedButton(onPressed: addItem, child: Text("Add Item")),
SizedBox(height: 20),
Text(message, style: TextStyle(color: Colors.red)),
SizedBox(height: 20),
Text("Total Price: $total", style: TextStyle(fontSize: 18)),
],
),
),
);
}
}Hotel Room Booking System
This section covers the navigation and data entry logic for a Hotel Room Booking application, utilizing Navigator to manage multiple screens.
import 'package:flutter/material.dart';
void main() { runApp(MyApp()); }
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: HomeScreen());
}
}
class Room {
String number, type, floor;
double rate;
Room(this.number, this.type, this.floor, this.rate);
}
List<Room> roomList = [];
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Hotel Room Booking")),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
ElevatedButton(onPressed: () { Navigator.push(context, MaterialPageRoute(builder: (context) => InsertScreen())); }, child: Text("Insert Room")),
ElevatedButton(onPressed: () { Navigator.push(context, MaterialPageRoute(builder: (context) => UpdateScreen())); }, child: Text("Holiday Surcharge")),
ElevatedButton(onPressed: () { Navigator.push(context, MaterialPageRoute(builder: (context) => DisplayScreen())); }, child: Text("Display Rooms")),
],
),
),
);
}
}
class InsertScreen extends StatefulWidget {
@override
State<InsertScreen> createState() => _InsertScreenState();
}
class _InsertScreenState extends State<InsertScreen> {
TextEditingController number = TextEditingController();
TextEditingController type = TextEditingController();
TextEditingController floor = TextEditingController();
TextEditingController rate = TextEditingController();
String message = "";
void addRoom() {
if (number.text.isEmpty || type.text.isEmpty || floor.text.isEmpty || rate.text.isEmpty) {
setState(() { message = "All fields are required"; });
return;
}
double r = double.tryParse(rate.text) ?? 0;
if (r <= 0) {
setState(() { message = "Enter valid rate"; });
return;
}
roomList.add(Room(number.text, type.text, floor.text, r));
}
}