1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import 'dart:collection';
- import 'package:flutter/material.dart';
- import 'package:flutter_provider_demo/dao/car_dao.dart';
- import 'package:flutter_provider_demo/model/car_model.dart';
- /// Description: car provider
- /// Time : 09/04/2023 Monday
- /// Author : liuyuqi.gov@msn.cn
- class CarProvider extends ChangeNotifier {
- late List<CarModel> _cars;
- int _count = 0;
- int get count => _count;
- UnmodifiableListView<CarModel> get allCars => UnmodifiableListView(_cars);
- UnmodifiableListView<CarModel> get unStartedCars =>
- UnmodifiableListView(_cars.where((car) => car.start == false));
- UnmodifiableListView<CarModel> get startedCars =>
- UnmodifiableListView(_cars.where((car) => car.start == true));
- Future<List<CarModel>> getCars() async {
- _cars = await CarDao.getCars();
- notifyListeners();
- return _cars;
- }
- void deleteCar(BuildContext context, CarModel car) async {
- await CarDao.deleteCar(car);
- _cars.remove(car);
- notifyListeners();
- }
- /// add car
- void addCar(CarModel car) async {
- final id = await CarDao.addCar(car);
- if (id > 0) {
- _cars.add(car);
- notifyListeners();
- }
- }
- void _showSnackBar(BuildContext context, String message) {
- final snackBar = SnackBar(
- duration: const Duration(milliseconds: 500),
- content: Text(
- message,
- style: const TextStyle(color: Colors.white),
- ),
- backgroundColor: Theme.of(context).primaryColor,
- );
- ScaffoldMessenger.of(context).showSnackBar(snackBar);
- }
- void startCar(BuildContext context, CarModel car) {
- notifyListeners();
- }
- }
|