12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import 'package:flutter/material.dart';
- import 'package:flutter_provider_demo/model/car_model.dart';
- import 'package:flutter_provider_demo/provider/car_provider.dart';
- import 'package:provider/provider.dart';
- class CarList extends StatelessWidget {
- final List<CarModel> cars;
- const CarList({super.key, required this.cars});
- @override
- Widget build(BuildContext context) {
- return ListView(
- children: getChildrenCars(),
- );
- }
- List<CarListItem> getChildrenCars() {
- return cars
- .map((car) => CarListItem(
- car: car,
- ))
- .toList();
- }
- }
- class CarListItem extends StatelessWidget {
- final CarModel car;
- const CarListItem({super.key, required this.car});
- @override
- Widget build(BuildContext context) {
- return ListTile(
- leading: Checkbox(
- value: car.start,
- onChanged: (bool? value) {
- Provider.of<CarProvider>(context, listen: false)
- .startCar(context, car);
- },
- ),
- title: Text("${car.brand} ${car.type}"),
- trailing: IconButton(
- onPressed: () {
- Provider.of<CarProvider>(context, listen: false)
- .deleteCar(context, car);
- },
- icon: Icon(
- Icons.delete,
- color: Theme.of(context).colorScheme.secondary,
- ),
- ),
- );
- }
- }
|