Initial draft
This commit is contained in:
75
lib/feature/cars/presentation/car_card.dart
Normal file
75
lib/feature/cars/presentation/car_card.dart
Normal file
@ -0,0 +1,75 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../model/car.dart';
|
||||
import 'car_dialog.dart';
|
||||
|
||||
class CarCard extends StatelessWidget {
|
||||
final Car car;
|
||||
final Function(Car car) onDelete;
|
||||
final Function(Car car, String newName) onEdit;
|
||||
|
||||
const CarCard({
|
||||
super.key,
|
||||
required this.car,
|
||||
required this.onEdit,
|
||||
required this.onDelete,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 10),
|
||||
child: Icon(
|
||||
Icons.local_shipping,
|
||||
size: 32,
|
||||
color: Theme.of(context).primaryColor,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(left: 10),
|
||||
child: Text(car.plate),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return CarDialog(
|
||||
onAction: (plate) {
|
||||
onEdit(car, plate);
|
||||
},
|
||||
action: CarAction.edit,
|
||||
initialPlateValue: car.plate,
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
icon: Icon(Icons.edit, color: Theme.of(context).primaryColor),
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
onDelete(car);
|
||||
},
|
||||
icon: const Icon(Icons.delete, color: Colors.redAccent),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
92
lib/feature/cars/presentation/car_dialog.dart
Normal file
92
lib/feature/cars/presentation/car_dialog.dart
Normal file
@ -0,0 +1,92 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
enum CarAction { edit, add }
|
||||
|
||||
class CarDialog extends StatefulWidget {
|
||||
const CarDialog({
|
||||
super.key,
|
||||
required this.onAction,
|
||||
this.action = CarAction.add,
|
||||
this.initialPlateValue = "",
|
||||
});
|
||||
|
||||
final CarAction action;
|
||||
final String initialPlateValue;
|
||||
final Function(String) onAction;
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _CarDialogState();
|
||||
}
|
||||
|
||||
class _CarDialogState extends State<CarDialog> {
|
||||
bool _isLoading = false;
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
late final TextEditingController _plateController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_plateController = TextEditingController.fromValue(
|
||||
TextEditingValue(text: widget.initialPlateValue),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
content: SizedBox(
|
||||
height: 120,
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 10.0),
|
||||
child: Text(
|
||||
"Fahrzeug ${widget.action == CarAction.add ? "hinzufügen" : "bearbeiten"}",
|
||||
style: const TextStyle(
|
||||
fontSize: 18.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
TextFormField(
|
||||
validator: (value) {
|
||||
if (value == "") {
|
||||
return "Kennzeichen darf nicht leer sein";
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
decoration: const InputDecoration(labelText: "Kennzeichen"),
|
||||
controller: _plateController,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
OutlinedButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: const Text("Abbrechen"),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
widget.onAction(_plateController.text);
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
},
|
||||
child: Text(
|
||||
widget.action == CarAction.add ? "Hinzufügen" : "Bearbeiten",
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
78
lib/feature/cars/presentation/car_management.dart
Normal file
78
lib/feature/cars/presentation/car_management.dart
Normal file
@ -0,0 +1,78 @@
|
||||
import 'package:hl_lieferservice/feature/cars/presentation/car_card.dart';
|
||||
|
||||
import '../../../model/car.dart';
|
||||
import 'car_dialog.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class CarManagementOverview extends StatefulWidget {
|
||||
final List<Car> cars;
|
||||
final Function(String plate) onAdd;
|
||||
final Function(String id) onDelete;
|
||||
final Function(String id, String plate) onEdit;
|
||||
|
||||
const CarManagementOverview({
|
||||
super.key,
|
||||
required this.cars,
|
||||
required this.onDelete,
|
||||
required this.onEdit,
|
||||
required this.onAdd,
|
||||
});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _CarManagementOverviewState();
|
||||
}
|
||||
|
||||
class _CarManagementOverviewState extends State<CarManagementOverview> {
|
||||
void _addCar() async {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return CarDialog(onAction: widget.onAdd);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _removeCar(Car car) async {
|
||||
widget.onDelete(car.id.toString());
|
||||
}
|
||||
|
||||
void _editCar(Car car, String newName) async {
|
||||
widget.onEdit(car.id.toString(), newName);
|
||||
}
|
||||
|
||||
Widget _buildCarOverview() {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(10),
|
||||
child: widget.cars.isEmpty ? const Center(child: Text("keine Fahrzeuge vorhanden")) : ListView.builder(
|
||||
itemBuilder:
|
||||
(context, index) => CarCard(
|
||||
car: widget.cars[index],
|
||||
onEdit: _editCar,
|
||||
onDelete: _removeCar,
|
||||
),
|
||||
itemCount: widget.cars.length,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
"Fahrzeugverwaltung",
|
||||
style: Theme.of(context).textTheme.headlineMedium,
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _addCar,
|
||||
backgroundColor: Theme.of(context).primaryColor,
|
||||
child: Icon(
|
||||
Icons.add,
|
||||
color: Theme.of(context).colorScheme.onSecondary,
|
||||
),
|
||||
),
|
||||
body: _buildCarOverview(),
|
||||
);
|
||||
}
|
||||
}
|
||||
82
lib/feature/cars/presentation/car_management_page.dart
Normal file
82
lib/feature/cars/presentation/car_management_page.dart
Normal file
@ -0,0 +1,82 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/authentication/bloc/auth_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/authentication/bloc/auth_state.dart';
|
||||
import 'package:hl_lieferservice/feature/cars/bloc/cars_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/cars/bloc/cars_event.dart';
|
||||
import 'package:hl_lieferservice/feature/cars/bloc/cars_state.dart';
|
||||
import 'package:hl_lieferservice/feature/cars/presentation/car_management.dart';
|
||||
import 'package:hl_lieferservice/model/car.dart';
|
||||
|
||||
class CarManagementPage extends StatefulWidget {
|
||||
const CarManagementPage({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _CarManagementPageState();
|
||||
}
|
||||
|
||||
class _CarManagementPageState extends State<CarManagementPage> {
|
||||
late Authenticated _authState;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
// Load cars
|
||||
_authState = context.read<AuthBloc>().state as Authenticated;
|
||||
context.read<CarsBloc>().add(CarLoad(teamId: _authState.teamId));
|
||||
}
|
||||
|
||||
void _add(String plate) {
|
||||
context.read<CarsBloc>().add(
|
||||
CarAdd(teamId: _authState.teamId, plate: plate),
|
||||
);
|
||||
}
|
||||
|
||||
void _remove(String id) {
|
||||
context.read<CarsBloc>().add(
|
||||
CarDelete(carId: id, teamId: _authState.teamId),
|
||||
);
|
||||
}
|
||||
|
||||
void _edit(String id, String plate) {
|
||||
context.read<CarsBloc>().add(
|
||||
CarEdit(
|
||||
newCar: Car(id: int.parse(id), plate: plate),
|
||||
teamId: _authState.teamId,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: BlocBuilder<CarsBloc, CarsState>(
|
||||
builder: (context, state) {
|
||||
debugPrint('BlocBuilder rebuilding with state: $state');
|
||||
|
||||
if (state is CarsLoading) {
|
||||
return Center(child: const CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (state is CarsLoaded) {
|
||||
return CarManagementOverview(
|
||||
cars: state.cars,
|
||||
onEdit: _edit,
|
||||
onAdd: _add,
|
||||
onDelete: _remove,
|
||||
);
|
||||
}
|
||||
|
||||
if (state is CarsLoadingFailed) {
|
||||
return Center(
|
||||
child: const Text("Fahrzeuge konnten nicht geladen werden"),
|
||||
);
|
||||
}
|
||||
|
||||
return Container();
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user