Tartalomjegyzék

< Flutter

Flutter triangle

main.dart

lib/main.dart
import 'package:flutter/material.dart';
import 'package:triangle/screens/mainwindow.dart';
 
void main() {
  runApp(Triangle());
}
 
class Triangle extends StatelessWidget {
  Triangle({super.key});
 
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: const Mainwindow(),
      theme: calcTheme,
    );
  }
final ThemeData calcTheme = ThemeData(        
        inputDecorationTheme: const InputDecorationTheme(          
          border: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.blueAccent),
            borderRadius: BorderRadius.all(Radius.circular(15.0)),          
          ),          
        ),
        textTheme: const TextTheme(
          bodyMedium : TextStyle(fontSize: 24.0),
          titleLarge: TextStyle(fontSize: 40.0),
          titleMedium: TextStyle(fontSize: 24.0),          
        ),        
      );
}

mainwindow.dart

lib/screen/mainwindow.dart
import 'package:flutter/material.dart';
 
class Mainwindow extends StatefulWidget {
  const Mainwindow({super.key});
  @override
  State<StatefulWidget> createState() {
    return MainState();
  }
}
 
class MainState extends State<Mainwindow>{
  TextEditingController baseController = TextEditingController();
  TextEditingController heightController = TextEditingController();
  TextEditingController areaController = TextEditingController();
  onPressed() {
    setState(() {
      startCalc();
    });
  }
  startCalc() {
    double base = double.parse(baseController.text);
    double height = double.parse(heightController.text);
    double area = calcArea(base, height);
    areaController.text = area.toString();
  }
  calcArea(double base, double height) {
    return base * height / 2;
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Háromszög')),
      body: Column(children: [
        const Text('Háromszög területszámítás'),
        const Text('Alap'),
        TextField(controller: baseController),
        const Text('Magasság'),
        TextField(controller: heightController),
        Row(
          children: [
            Expanded(child: 
              ElevatedButton(
                onPressed: onPressed, 
                child: const Text('Számít')
              )
            ),
          ],
        ),
        Text(areaController.text),
 
      ]),
    );
  }
}

Eredmény