Flutter Room Management App Code Refinement
Code Cleanup and Structure Improvement
The provided code snippets appear to be fragmented parts of a Flutter application, likely dealing with room management (insertion and updates). Below is a structured and corrected presentation of the logic, assuming necessary external definitions like Room, roomList, and controller initializations exist.
Room Insertion Logic Refinement
This section focuses on adding a new room entry.
Widget for Adding a New Room
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Insert Room")),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: number,
decoration: InputDecoration(labelText: "Room Number"),
),
TextField(
controller: type,
decoration: InputDecoration(labelText: "Room Type"),
),
TextField(
controller: floor,
decoration: InputDecoration(labelText: "Floor"),
),
TextField(
controller: rate,
keyboardType: TextInputType.number,
decoration: InputDecoration(labelText: "Nightly Rate"),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: addRoom,
child: Text("Save Room")
),
SizedBox(height: 20),
Text(message, style: TextStyle(color: Colors.red)),
],
),
),
);
}
void addRoom() {
// Assuming clear operations happen here or upon success
number.clear();
type.clear();
floor.clear();
rate.clear();
setState(() {message = "Room Added";});
}
Room Listing and Display
This snippet shows how to display the list of rooms using ListView.builder.
Building the Room List View
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Room List")),
body: ListView.builder(
itemCount: roomList.length,
itemBuilder: (context, index) {
return ListTile(
title: Text("Room ${roomList[index].number}"),
subtitle: Text(
"Type: ${roomList[index].type}\n"
"Rate: ${roomList[index].rate}"
),
);
},
),
);
}
Holiday Surcharge Update Logic
This section handles applying a 25% surcharge to a specific room’s rate.
UpdateScreen State Management
class UpdateScreen extends StatefulWidget {
@override
State<UpdateScreen> createState() => _UpdateScreenState();
}
class _UpdateScreenState extends State<UpdateScreen> {
TextEditingController number = TextEditingController();
String message = "";
void applySurcharge() {
if (number.text.isEmpty) {
setState(() { message = "Enter Room Number"; });
return;
}
Room? r;
for (var e in roomList) {
if (e.number == number.text) {
r = e;
break;
}
}
if (r == null) {
setState(() { message = "Room Not Found"; });
return;
}
r.rate = r.rate * 1.25;
setState(() {message = "Rate Updated";});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Holiday Surcharge")),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: number,
decoration: InputDecoration(labelText: "Room Number"),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: applySurcharge,
child: Text("Apply 25% Increase"),
),
SizedBox(height: 20),
Text(message, style: TextStyle(color: Colors.red),),
],
),
),
);
}
}
/* --- DISPLAY --- */
class DisplayScreen extends StatelessWidget {
@override
// ... rest of DisplayScreen implementation
}