Complete Flutter App Structure Explained

Flutter apps are built using the Dart programming language and revolve around the concept of Widgets, which compose the user interface. Below is a full basic Flutter app structure with detailed explanation of each part, why it exists, and how it works.

Full Flutter App Code

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo App',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pressed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Breaking down the code:

Why these parts? See Full Guide

StatelessWidget vs StatefulWidget: StatelessWidgets are immutable and used when UI doesn't change dynamically. StatefulWidgets can rebuild with updated data or user interaction.

MaterialApp: Provides fundamental app structure, theming, navigation support, localization, and more out of the box.

Scaffold: Gives you basic app visual structure with app bars, drawers, floating buttons, and body content, saving time to build these common parts manually.

setState: Notifies Flutter that the internal state changed so the framework knows to rebuild widgets with new data, refreshing the UI.

How does Flutter render this?

Flutter uses a reactive programming model. When setState() is called, Flutter schedules a rebuild of widgets below that widget in the tree, updating what is rendered on screen. Widgets themselves are lightweight and immutable; the framework efficiently manages rendering differences.

Summary

Further Reading & Official References