Understanding single-page flutter app with state

// Imports Flutter's Material Design widgets such as

// Scaffold, Text, Button, AppBar, etc.
import 'package:flutter/material.dart';
// Every Dart application starts from main()
void main() {
  // Starts the Flutter application.
  // MyApp becomes the root widget.
  runApp(const MyApp());
}
// Root widget of our application.
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    // MaterialApp provides the basic app configuration.
    return MaterialApp(
      // Removes the DEBUG banner.
      debugShowCheckedModeBanner: false,
      // HomePage is the first screen.
      home: const HomePage(),
    );
  }
}
// StatefulWidget is used because the counter will change.
class HomePage extends StatefulWidget {
  const HomePage({super.key});
  @override
  State<HomePage> createState() => _HomePageState();
}
// This class stores the changing state of HomePage.
class _HomePageState extends State<HomePage> {
  // Variable holding our current counter value.
  int counter = 0;
  // Function called when the button is pressed.
  void incrementCounter() {
    // setState tells Flutter:
    // "My data changed. Rebuild the UI."
    setState(() {
      counter++; // Same as: counter = counter + 1;
    });
  }
  @override
  Widget build(BuildContext context) {
    // Scaffold creates a basic page structure.
    return Scaffold(
      // Top bar of the page.
      appBar: AppBar(
        title: const Text('My First Flutter App'),
      ),
      // Main content of the page.
      body: Center(
        // Column places widgets vertically.
        child: Column(
          // Makes the Column only as tall as necessary.
          mainAxisSize: MainAxisSize.min,
          // Column can contain multiple widgets using children.
          children: [
            // Displays a Flutter icon.
            const Icon(
              Icons.favorite,
              color: Colors.red,
              size: 60,
            ),
            // Adds vertical space.
            const SizedBox(height: 20),
            // Displays normal text.
            const Text(
              'Welcome to Flutter!',
              style: TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 20),
            // $counter inserts the value of counter into the String.
            Text(
              'Button clicked $counter times',
            ),
            const SizedBox(height: 20),
            // Creates a button.
            ElevatedButton(
              // When clicked, call incrementCounter().
              onPressed: incrementCounter,
              // Text displayed inside the button.
              child: const Text('Click Me'),
            ),
          ],
        ),
      ),
    );
  }
}

Remember only this flow

Button click → incrementCounter() → setState() → counter++ → UI rebuilds


Comments

Popular posts from this blog

Files in your Flutter Project

Flutter setup on Windows 11 using VS Code + Android Emulator