123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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: 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<CarProvider>(context, listen: false).addCar(car);
- Navigator.pop(context);
- }
- }
- }
|