addpage_page.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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: [],
  22. title: Text("增加车辆"),
  23. ),
  24. body: ListView(children: [
  25. Padding(
  26. padding: EdgeInsets.all(25),
  27. child: Column(
  28. children: [
  29. TextField(
  30. controller: carBrandController,
  31. ),
  32. TextField(
  33. controller: carTypeController,
  34. ),
  35. CheckboxListTile(
  36. value: started,
  37. onChanged: (checked) {
  38. setState(() {
  39. started = checked;
  40. });
  41. },
  42. title: const Text('Start ?'),
  43. ),
  44. RaisedButton(
  45. elevation: 5.0,
  46. child: Text('Add'),
  47. onPressed: onAdd,
  48. shape: RoundedRectangleBorder(
  49. borderRadius: BorderRadius.circular(30.0)),
  50. ),
  51. ],
  52. crossAxisAlignment: CrossAxisAlignment.stretch,
  53. ),
  54. )
  55. ]),
  56. );
  57. }
  58. @override
  59. void dispose() {
  60. super.dispose();
  61. carBrandController.dispose();
  62. }
  63. /// add car
  64. void addCar(int id) {
  65. if (carBrandController.text.isNotEmpty &&
  66. carTypeController.text.isNotEmpty) {
  67. final CarModel car = CarModel(
  68. id: id,
  69. brand: carBrandController.text,
  70. type: carTypeController.text,
  71. start: started);
  72. Provider.of<CarProvider>(context, listen: false).addCar(car);
  73. Navigator.pop(context);
  74. }
  75. }
  76. }