Understand Flutter main.dart file

  1. import 'package:flutter/material.dart';
  2. It imports flutter material package. It allows to create UI based on material design guideline specified by Android.

  3. void main() { runApp(MyApp()); }
  4. It is the main entry point of the flutter project. It calls the runApp method. Pass object of MyApp class which will be stateful or stateless widget.

  5. class MyApp extends StatelessWidget { . . . }
  6. Here, MyApp is a Stateless widget. Stateless widget is a widget which does not maintain any state of the widget.

  7. @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: MyHomePage(title: 'Flutter Demo Home Page'),
      );
    }

  8. MyApp class overrides the build method which is used to create UI. It returns MaterialApp which is the root Widget of the application UI. It has following properties:

    title: It is title of application.
    theme: It is theme of the application. It uses ThemeData to configure application theme.
    primarySwatch: Its type is MaterialColor.
    visualDensity: VisualDensity.adaptivePlatformDensity: This makes the visual density adapt to the platform that you run the app on. For desktop platforms, the controls will be smaller and closer together (more dense) than on mobile platforms.
    home: It sets the inner UI of the application which is another widget MyHomePage.
    MyHomePage is a Stateful widget which takes title as a parameter of constructor. It uses Scaffold, AppBar, Center, Text and many other widgets which we will learn in detail in further topics.