carlist.dart 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_provider_demo/model/car_model.dart';
  3. import 'package:flutter_provider_demo/provider/car_provider.dart';
  4. import 'package:provider/provider.dart';
  5. class CarList extends StatelessWidget {
  6. final List<CarModel> cars;
  7. const CarList({super.key, required this.cars});
  8. @override
  9. Widget build(BuildContext context) {
  10. return ListView(
  11. children: getChildrenCars(),
  12. );
  13. }
  14. List<CarListItem> getChildrenCars() {
  15. return cars
  16. .map((car) => CarListItem(
  17. car: car,
  18. ))
  19. .toList();
  20. }
  21. }
  22. class CarListItem extends StatelessWidget {
  23. final CarModel car;
  24. const CarListItem({super.key, required this.car});
  25. @override
  26. Widget build(BuildContext context) {
  27. return ListTile(
  28. leading: Checkbox(
  29. value: car.start,
  30. onChanged: (bool? value) {
  31. Provider.of<CarProvider>(context, listen: false)
  32. .startCar(context, car);
  33. },
  34. ),
  35. title: Text("${car.brand} ${car.type}"),
  36. trailing: IconButton(
  37. onPressed: () {
  38. Provider.of<CarProvider>(context, listen: false)
  39. .deleteCar(context, car);
  40. },
  41. icon: Icon(
  42. Icons.delete,
  43. color: Theme.of(context).colorScheme.secondary,
  44. ),
  45. ),
  46. );
  47. }
  48. }