Introduction to Flutter and Dart Code

  1. Important points about Dart and Flutter code
  2. Exploring “main.dart”

This lab introduces some of the basic elements of Flutter and Dart.

Important points about Dart and Flutter code

The Flutter SDK is written in the Dart programming language. Dart is an object-oriented language created by Google.

Dart looks and behaves like a combination of Python and Java. You will become familiar with Dart throughout the semester, but hopefully you have enough experience with programming by now to pick up the language.

Here are a few important points about Dart and Flutter code you should know before we proceed:

Exploring “main.dart”

We will take a look at the file lib/main.dart in a newly-created Flutter project.

The line import 'package:flutter/material.dart'; is a Dart statement (note how it ends with a semicolon) that imports Flutter classes for use in the source file.

The following code block is the starting point for the Flutter app when it runs on a device. The main() method.

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

Use the following diagram to help organize the classes/widgets that build the screen:

Diagram of the basic flutter app

You should see three classes defined in main.dart: MyApp, MyHomePage, and _MyHomePageState. These classes are used to draw what you see on your device’s screen. We will talk about what these classes do and how they are specified later. For now, I want you to observe a few things:

If you look closely in the build() method of MyApp, you can see a reference to the MyHomePage widget. The MyHomePage widget references the _MyHomePageState class. The building-blocks of a Flutter application, like most GUI libraries, follows a tree-structure. There is a “root” widget that represents the app on the screen – in this case MyApp. MyApp specifies some global information about your app, including the app’s title and which theme to use (the theme optionally specifies a color scheme, fonts, and other things).

MyApp “contains” the MyHomePage widget as a child. This widget references _MyHomePageState, which actually draws things on the screen. The build(...) method in _MyHomePageState is what is actually specifying the things you see on the screen.

A lot of this is probably overwhelming, and that’s okay at this point. We will get into it all. The most important thing to start to orient in your mind is that: