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 createState() => _CarsState(); } class _CarsState extends State { 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) ]; @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: [ const TextField( decoration: InputDecoration( labelText: 'Nazwa', ), ), const TextField( decoration: InputDecoration( labelText: 'Rocznik', ), ), const TextField( decoration: InputDecoration( labelText: 'Kolor', ), ), const TextField( decoration: InputDecoration( labelText: 'Przebieg', ), ), TextButton( onPressed: () { setState(() { }); }, child: Container( margin: 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.red, 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(10), height: 150, 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), ) ], ) ), ); } ), ), ], ), ) ) ); } }