To implement a list of items with their corresponding quantities and units in your Flutter web application, we will modify the structure of each item in the list to include a name (with mixed text and numbers), a quantity, and a selectable unit. We'll use a TextField for the item name, a TextField for the quantity, and a DropdownButton for the units.
Here’s how you can set this up:
Step 1: Create a Data Model for Items
First, let's create a simple data model to represent an item. Create a new Dart file item_model.dart.
lib/item_model.dart
class Item {
String name;
double quantity;
String unit;
Item({required this.name, required this.quantity, required this.unit});
}
Step 2: Update add_item.dart
We will now update the add_item.dart to include TextField for item names, quantities, and a DropdownButton for units.
Updated add_item.dart
// lib/add_item.dart
import 'package:flutter/material.dart';
import 'item_model.dart';
class AddItem extends StatefulWidget {
@override
_AddItemState createState() => _AddItemState();
}
class _AddItemState extends State<AddItem> {
final TextEditingController _nameController = TextEditingController();
final TextEditingController _qtyController = TextEditingController();
String _selectedUnit = 'g';
final List<String> _units = ['g', 'kg', 'ml', 'L', 'Other'];
@override
void dispose() {
_nameController.dispose();
_qtyController.dispose();
super.dispose();
}
void _addItem() {
final String name = _nameController.text;
final double? qty = double.tryParse(_qtyController.text);
final String unit = _selectedUnit;
if (name.isNotEmpty && qty != null) {
Item newItem = Item(name: name, quantity: qty, unit: unit);
// Here you can add logic to save the new item, for example, to a list in the parent widget
Navigator.pop(context); // Go back after adding the item
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Please enter valid name and quantity')),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Add Item'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: _nameController,
decoration: InputDecoration(
labelText: 'Item Name',
hintText: 'e.g. Apples 1kg',
),
),
TextField(
controller: _qtyController,
decoration: InputDecoration(
labelText: 'Quantity',
hintText: 'e.g. 1.5',
),
keyboardType: TextInputType.number,
),
DropdownButton<String>(
value: _selectedUnit,
onChanged: (String? newValue) {
setState(() {
_selectedUnit = newValue!;
});
},
items: _units.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _addItem,
child: Text('Add Item'),
),
],
),
),
);
}
}
Step 3: Update edit_item.dart
Similarly, you will need to allow users to edit the item data, which can be done in the edit_item.dart file. Here’s an outline for that, assuming you want similar functionality as in add_item.dart.
Updated edit_item.dart
// lib/edit_item.dart
import 'package:flutter/material.dart';
import 'item_model.dart';
class EditItem extends StatefulWidget {
final Item item;
EditItem({required this.item});
@override
_EditItemState createState() => _EditItemState();
}
class _EditItemState extends State<EditItem> {
late TextEditingController _nameController;
late TextEditingController _qtyController;
late String _selectedUnit;
final List<String> _units = ['g', 'kg', 'ml', 'L', 'Other'];
@override
void initState() {
_nameController = TextEditingController(text: widget.item.name);
_qtyController = TextEditingController(text: widget.item.quantity.toString());
_selectedUnit = widget.item.unit;
super.initState();
}
@override
void dispose() {
_nameController.dispose();
_qtyController.dispose();
super.dispose();
}
void _editItem() {
final String name = _nameController.text;
final double? qty = double.tryParse(_qtyController.text);
final String unit = _selectedUnit;
if (name.isNotEmpty && qty != null) {
Item updatedItem = Item(name: name, quantity: qty, unit: unit);
// Here you can add logic to update the item in your list
Navigator.pop(context); // Go back after editing the item
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Please enter valid name and quantity')),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Edit Item'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
TextField(
controller: _nameController,
decoration: InputDecoration(
labelText: 'Item Name',
),
),
TextField(
controller: _qtyController,
decoration: InputDecoration(
labelText: 'Quantity',
),
keyboardType: TextInputType.number,
),
DropdownButton<String>(
value: _selectedUnit,
onChanged: (String? newValue) {
setState(() {
_selectedUnit = newValue!;
});
},
items: _units.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _editItem,
child: Text('Save Item'),
),
],
),
),
);
}
}
Step 4: Update list_items.dart
When displaying the items in your list_items.dart, you'll need to modify the structure to match your new Item model. You'll need to maintain a list of Items, allowing you to edit, add, and presumably delete items from it.
list_items.dart
Here's a revised structure for the ListItems screen:
// lib/list_items.dart
import 'package:flutter/material.dart';
import 'add_item.dart';
import 'edit_item.dart';
import 'delete_item.dart';
import 'item_model.dart';
class ListItems extends StatefulWidget {
@override
_ListItemsState createState() => _ListItemsState();
}
class _ListItemsState extends State<ListItems> {
List<Item> items = []; // Store list of items
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Items List'),
actions: [
IconButton(
icon: Icon(Icons.add),
tooltip: 'Add Item',
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AddItem()),
);
},
),
],
),
body: ListView.builder(
itemCount: items.length, // Update this to items.length
itemBuilder: (context, index) {
Item item = items[index]; // Get the item
return ListTile(
title: Text(item.name),
subtitle: Text('Quantity: ${item.quantity} ${item.unit}'),
trailing: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(Icons.edit),
tooltip: 'Edit Item',
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => EditItem(item: item)),
).then((_) {
setState(() {}); // refresh the state when returning
});
},
),
IconButton(
icon: Icon(Icons.delete),
tooltip: 'Delete Item',
onPressed: () {
// Logic to delete item
setState(() {
items.removeAt(index);
});
},
),
],
),
);
},
),
);
}
}
Summary of Changes
- Item Model: Created an
Item model to hold item details.
- Add Item Screen: Created a screen allowing users to add items with mixed text/numbers, a quantity (with decimals), and a selectable unit.
- Edit Item Screen: Created an editable form for the existing items.
- List Screen: Updated to show the items’ names, quantities, and units while incorporating add, edit, and delete functionalities.
Final Steps
Make sure you run your application:
flutter run -d chrome
You should now see a fully functional item management system that allows users to add items with names, quantities, and units, edit existing items, and delete items. Feel free to expand this as per your application needs! If you require any more modifications or additional features, let me know!