31.3 Flutter App Structure

20210407 The Dart code for a Flutter app belongs in lib/main.dart. This will usually begin with import statements to load up the required libraries. It then continues with a main() function which is invoked when the app starts up. The main() function of our minimal Flutter app calls the runApp() function which takes as its argument a Widget which becomes the root of the widget tree.

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Material(
        color: Colors.blue,
        child: Center(
          child: Text(
            'Hello World',
            style: TextStyle(fontSize: 40, color: Colors.white),
          ),
        ),
      ),
    ),
  );
}

The MaterialApp class has a home: property which takes a widget which is to be the default route of the app (/), and is displayed when the application is started. A Material widget is defined with a color specified and a child widget Center. The Center widget aligns its own child widget to the center of available screen space. In our case here that child widget is Text, taking a string to be displayed and specifying the text style using the TextStyle class.



Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.