addpage_page.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /// Description: add cars
  6. /// Time : 09/04/2023 Monday
  7. /// Author : liuyuqi.gov@msn.cn
  8. class AddcardPage extends StatefulWidget {
  9. const AddcardPage({super.key});
  10. @override
  11. State<AddcardPage> createState() => _AddcardPageState();
  12. }
  13. class _AddcardPageState extends State<AddcardPage> {
  14. final carBrandController = TextEditingController();
  15. final carTypeController = TextEditingController();
  16. bool started = false;
  17. @override
  18. Widget build(BuildContext context) {
  19. return Scaffold(
  20. appBar: AppBar(
  21. actions: const [],
  22. title: const Text("增加车辆"),
  23. ),
  24. body: ListView(children: [
  25. Padding(
  26. padding: const EdgeInsets.all(25),
  27. child: Column(
  28. crossAxisAlignment: CrossAxisAlignment.stretch,
  29. children: [
  30. TextField(
  31. controller: carBrandController,
  32. ),
  33. TextField(
  34. controller: carTypeController,
  35. ),
  36. CheckboxListTile(
  37. value: started,
  38. onChanged: (checked) {
  39. setState(() {
  40. started = checked ?? false;
  41. });
  42. },
  43. title: const Text('Start ?'),
  44. ),
  45. ElevatedButton(
  46. onPressed: () {
  47. addCar(1);
  48. },
  49. style: ElevatedButton.styleFrom(
  50. backgroundColor: Colors.blue,
  51. textStyle: const TextStyle(fontSize: 20)),
  52. child: const Text('Add'),
  53. ),
  54. ],
  55. ),
  56. )
  57. ]),
  58. );
  59. }
  60. @override
  61. void dispose() {
  62. super.dispose();
  63. carBrandController.dispose();
  64. }
  65. /// add car
  66. void addCar(int id) {
  67. if (carBrandController.text.isNotEmpty &&
  68. carTypeController.text.isNotEmpty) {
  69. final CarModel car = CarModel(
  70. id: id,
  71. brand: carBrandController.text,
  72. type: carTypeController.text,
  73. start: started);
  74. Provider.of<CarProvider>(context, listen: false).addCar(car);
  75. Navigator.pop(context);
  76. }
  77. }
  78. }