/
123321
/
qwery
Обзор
Документация
Войти
/
123321
/
qwery
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
1
125 строк
4 KB
123321
create 1
27 окт 2024, 11:59
27 окт 2024, 11:59
784b1ff
Код
Авторство
О чём код?
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Купить Корабль', home: ShipPurchaseScreen(), ); } } class Ship { final double capacity; final double horsepower; final double price; Ship(this.capacity, this.horsepower, this.price); } class ShipPurchaseScreen extends StatefulWidget { @override _ShipPurchaseScreenState createState() => _ShipPurchaseScreenState(); } class _ShipPurchaseScreenState extends State<ShipPurchaseScreen> { final TextEditingController _balanceController = TextEditingController(); final TextEditingController _capacityController = TextEditingController(); final TextEditingController _horsepowerController = TextEditingController(); double _balance = 0.0; double _shipPrice = 0.0; List<Ship> _purchasedShips = []; void _calculateShipPrice() { final double capacity = double.tryParse(_capacityController.text) ?? 0.0; final double horsepower = double.tryParse(_horsepowerController.text) ?? 0.0; _shipPrice = (capacity * 100000) + (horsepower * 50000); } void _purchaseShip() { _calculateShipPrice(); if (_balance >= _shipPrice) { setState(() { _balance -= _shipPrice; _purchasedShips.add(Ship( double.tryParse(_capacityController.text) ?? 0.0, double.tryParse(_horsepowerController.text) ?? 0.0, _shipPrice, )); _capacityController.clear(); _horsepowerController.clear(); }); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Корабль куплен за \$${_shipPrice.toStringAsFixed(2)}!')), ); } else { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Недостаточно средств!')), ); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Купи Корабль'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ TextField( controller: _balanceController, decoration: InputDecoration(labelText: 'Введите ваш баланс'), keyboardType: TextInputType.number, onChanged: (value) { _balance = double.tryParse(value) ?? 0.0; }, ), SizedBox(height: 20), TextField( controller: _capacityController, decoration: InputDecoration(labelText: 'Вместимость корабля'), keyboardType: TextInputType.number, ), TextField( controller: _horsepowerController, decoration: InputDecoration(labelText: 'Лошадиные силы'), keyboardType: TextInputType.number, ), SizedBox(height: 20), ElevatedButton( onPressed: _purchaseShip, child: Text('Купить Корабль'), ), SizedBox(height: 20), Text('Ваш баланс: \$${_balance.toStringAsFixed(2)}'), SizedBox(height: 20), Text('Купленные корабли:'), Expanded( child: ListView.builder( itemCount: _purchasedShips.length, itemBuilder: (context, index) { final ship = _purchasedShips[index]; return ListTile( title: Text('Корабль ${index + 1}'), subtitle: Text('Вместимость: ${ship.capacity}, ЛС: ${ship.horsepower}, Цена: \$${ship.price.toStringAsFixed(2)}'), ); }, ), ), ], ), ), ); } }