12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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<AddcardPage> createState() => _AddcardPageState();
- }
- class _AddcardPageState extends State<AddcardPage> {
- final carBrandController = TextEditingController();
- final carTypeController = TextEditingController();
- bool started = false;
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- actions: [],
- title: Text("增加车辆"),
- ),
- body: ListView(children: [
- Padding(
- padding: EdgeInsets.all(25),
- child: Column(
- children: [
- TextField(
- controller: carBrandController,
- ),
- TextField(
- controller: carTypeController,
- ),
- CheckboxListTile(
- value: started,
- onChanged: (checked) {
- setState(() {
- started = checked;
- });
- },
- title: const Text('Start ?'),
- ),
- RaisedButton(
- elevation: 5.0,
- child: Text('Add'),
- onPressed: onAdd,
- shape: RoundedRectangleBorder(
- borderRadius: BorderRadius.circular(30.0)),
- ),
- ],
- crossAxisAlignment: CrossAxisAlignment.stretch,
- ),
- )
- ]),
- );
- }
- @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<CarProvider>(context, listen: false).addCar(car);
- Navigator.pop(context);
- }
- }
- }
|