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'; /// Description: add cars /// Time : 09/04/2023 Monday /// Author : liuyuqi.gov@msn.cn class AddcardPage extends StatefulWidget { const AddcardPage({super.key}); @override State createState() => _AddcardPageState(); } class _AddcardPageState extends State { final carBrandController = TextEditingController(); final carTypeController = TextEditingController(); bool started = false; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( actions: const [], title: const Text("增加车辆"), ), body: ListView(children: [ Padding( padding: const EdgeInsets.all(25), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ TextField( controller: carBrandController, ), TextField( controller: carTypeController, ), CheckboxListTile( value: started, onChanged: (checked) { setState(() { started = checked ?? false; }); }, title: const Text('Start ?'), ), ElevatedButton( onPressed: () { addCar(1); }, style: ElevatedButton.styleFrom( backgroundColor: Colors.blue, textStyle: const TextStyle(fontSize: 20)), child: const Text('Add'), ), ], ), ) ]), ); } @override void dispose() { super.dispose(); carBrandController.dispose(); } /// add car void addCar(int id) { if (carBrandController.text.isNotEmpty && carTypeController.text.isNotEmpty) { final CarModel car = CarModel( id: id, brand: carBrandController.text, type: carTypeController.text, start: started); Provider.of(context, listen: false).addCar(car); Navigator.pop(context); } } }