car_provider.dart 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_provider_demo/dao/car_dao.dart';
  3. import 'package:flutter_provider_demo/model/car_model.dart';
  4. /// Description: car provider
  5. /// Time : 09/04/2023 Monday
  6. /// Author : liuyuqi.gov@msn.cn
  7. class CarProvider extends ChangeNotifier {
  8. late List<CarModel> _cars;
  9. int _count = 0;
  10. Future<List<CarModel>> getCars() async {
  11. _cars = await CarDao.getCars();
  12. notifyListeners();
  13. return _cars;
  14. }
  15. void deleteCar(BuildContext context, CarModel car) async {
  16. await CarDao.deleteCar(car);
  17. _cars.remove(car);
  18. notifyListeners();
  19. }
  20. /// add car
  21. void addCar(CarModel car) async {
  22. final id = await CarDao.addCar(car);
  23. if (id > 0) {
  24. _cars.add(car);
  25. notifyListeners();
  26. }
  27. }
  28. void _showSnackBar(BuildContext context, String message) {
  29. final snackBar = SnackBar(
  30. duration: const Duration(milliseconds: 500),
  31. content: Text(
  32. message,
  33. style: const TextStyle(color: Colors.white),
  34. ),
  35. backgroundColor: Theme.of(context).primaryColor,
  36. );
  37. ScaffoldMessenger.of(context).showSnackBar(snackBar);
  38. }
  39. }