👨‍🏫0.7 Условия - if(){} - else if(){} - else{} part 2

import 'package:flutter/material.dart';

class Lesson20 extends StatefulWidget {
  const Lesson20({super.key});

  @override
  State<Lesson20> createState() => _Lesson20State();
}

class _Lesson20State extends State<Lesson20> {
  TextEditingController moneyController = TextEditingController();
  double money = 500;

  String message = '';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          children: [
            Container(
              height: 127,
              width: double.infinity,
              padding: EdgeInsets.all(24),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                color: Colors.white,
                border: Border.all(color: Colors.black)
              ),
              child: Column(
                crossAxisAlignment: .start,
                children: [
                  Text('Balance'),
                  Text('$money сом', style: TextStyle(fontSize: 40),)
                ],
              ),
            ),
            SizedBox(height: 40,),
            TextFormField(
              controller: moneyController,
              decoration: InputDecoration(
                border: OutlineInputBorder()
              ),
            ),
            SizedBox(height: 40,),
            Text(message),
            SizedBox(height: 100,),
            ElevatedButton(
                style: ElevatedButton.styleFrom(
                  minimumSize: Size(double.infinity, 55),
                  shape: RoundedRectangleBorder(
                    borderRadius: .circular(8),
                  ),
                  backgroundColor: Colors.green
                ),
                onPressed: (){
                  // ==, !=, >, <, >=, <=,
                  if(moneyController.text.isEmpty){
                    message = 'Введите сумму';

                  } else if(double.tryParse(moneyController.text)! <= 0){
                    message = 'Сумма должен быть больше 0';
                  } else if(double.tryParse(moneyController.text)! > money){
                    message = 'Недостаточно средств';
                  } else if(double.tryParse(moneyController.text)! <= money){
                    message = 'оплата прошла';
                    money -= double.tryParse(moneyController.text)!;
                  } else{
                    message = 'Произошло неизвестная ошибка';
                  }

                  setState(() {

                  });

                }, child: Text('Оплатить'))
          ],
        ),
      ),
    );
  }
}

Last updated