Flutter – interaktivnost

Napraviti aplikaciju koja sabira dva broja.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Sabiranje',
      theme: ThemeData(

        colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Digitron'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  TextEditingController tcX = TextEditingController(text: "");
  TextEditingController tcY = TextEditingController(text: "");
  double x=0,y=0, sum=0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title)
      ),
      body: Center(
              child: Column(
                children: [
                  Text("Sabiranje", style: TextStyle(fontSize: 30),),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text("X:"),
                      SizedBox(width: 100, child: TextField(controller: tcX, textAlign: TextAlign.center,)),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text("Y:"),
                      SizedBox(width: 100, child: TextField(controller: tcY, textAlign: TextAlign.center,)),
                    ],
                  ),
                  SizedBox(height: 20,),
                  ElevatedButton(onPressed: addNumbers, child: Text("Saberi")),
                  SizedBox(height: 20,),
                  Text("Zbir: $sum", style: TextStyle(fontSize: 25),)
                ],
              )
        )
    );
  }

  void addNumbers() {
    x = double.parse(tcX.text);
    y = double.parse(tcY.text);
    sum = x + y;

    setState(() {

    });
  }
}