- import 'package:flutter/material.dart';
- import 'car.dart';
- void main() {
- runApp(const Cars());
- }
- class Cars extends StatefulWidget {
- const Cars({Key? key}) : super(key: key);
- @override
- State<Cars> createState() => _CarsState();
- }
- class _CarsState extends State<Cars> {
- List cars = [
- Car("Renault", 2018, "red", 89000),
- Car("Audi", 2019, "black", 45000),
- Car("Mazda", 2011, "blue", 186000),
- Car("Lexus", 2015, "white", 230000),
- Car("Opel", 2010, "black", 123000),
- Car("Volvo", 2018, "yellow", 20000)
- ];
- TextEditingController controllerA = TextEditingController();
- TextEditingController controllerB = TextEditingController();
- TextEditingController controllerC = TextEditingController();
- TextEditingController controllerD = TextEditingController();
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: Scaffold(
- body: SafeArea(
- child: Column(
- children: [
- Container(
- margin: EdgeInsets.fromLTRB(0, 20, 0, 20),
- padding: EdgeInsets.all(10),
- width: 400,
- decoration: BoxDecoration(
- border: Border.all(
- width: 3,
- color: Colors.brown
- )
- ),
- child: Column(
- children: [
- TextField(
- controller: controllerA,
- decoration: const InputDecoration(
- labelText: 'Nazwa',
- ),
- ),
- TextField(
- controller: controllerB,
- decoration: const InputDecoration(
- labelText: 'Rocznik',
- ),
- ),
- TextField(
- controller: controllerC,
- decoration: const InputDecoration(
- labelText: 'Kolor',
- ),
- ),
- TextField(
- controller: controllerD,
- decoration: const InputDecoration(
- labelText: 'Przebieg',
- ),
- ),
- TextButton(
- onPressed: () {
- setState(() {
- String name = controllerA.text;
- int year = int.parse(controllerB.text);
- String color = controllerC.text;
- int milleage = int.parse(controllerD.text);
- Car newCar = Car(name, year, color, milleage);
- cars.add(newCar);
- controllerA.clear();
- controllerB.clear();
- controllerC.clear();
- controllerD.clear();
- });
- },
- child: Container(
- margin: const EdgeInsets.fromLTRB(0, 20, 0, 10),
- alignment: Alignment.center,
- width: 400,
- height: 60,
- decoration: BoxDecoration(
- color: Colors.purpleAccent,
- borderRadius: BorderRadius.circular(5)
- ),
- child: const Text("Add", style: TextStyle(
- color: Colors.white,
- fontSize: 20
- )
- )
- )
- )
- ],
- )
- ),
- TextButton(
- onPressed: () {
- setState(() {
- cars.removeWhere((car) => car.isCrashed());
- });
- },
- child: Container(
- alignment: Alignment.center,
- width: 400,
- height: 60,
- decoration: BoxDecoration(
- color: Colors.grey[700],
- borderRadius: BorderRadius.circular(5)
- ),
- child: const Text("Remove", style: TextStyle(
- color: Colors.white,
- fontSize: 20
- )
- )
- )
- ),
- Expanded(
- child: ListView.builder(
- itemCount: cars.length,
- itemBuilder: (BuildContext context, int index) {
- return TextButton(
- onPressed: () {
- setState(() {
- cars[index].setCrashed();
- });
- },
- child: Container(
- margin: EdgeInsets.all(5),
- height: 100,
- width: 400,
- decoration: BoxDecoration(
- color: cars[index].isCrashed() ? Colors.amber[800] : Colors.amber,
- borderRadius: BorderRadius.circular(5)
- ),
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Text(cars[index].getName(), style: const TextStyle(
- color: Colors.redAccent,
- fontSize: 23,
- fontWeight: FontWeight.bold
- )
- ),
- Container(
- child: Text("rocznik " + cars[index].getYear().toString(), style: const TextStyle(color: Colors.brown)),
- margin: EdgeInsets.all(8),
- )
- ],
- )
- ),
- );
- }
- ),
- ),
- ],
- ),
- )
- )
- );
- }
- }