Creating your first .dart class


Our first .dart class is going to be pretty simple for example sake. We’re going to run our app and show some simple text on the screen.

You’ll notice that the screenshot on the left here displays a simple app that is just showing us some sample text in the center of the screen.

Reaching this result consists of three things:

  • Creating a Text Widget
  • Creating a Center widget to center the text
  • Calling the function to display our widgets

This can simply be achieved by the following snippet:

import 'package:flutter/material.dart';

void main() {
  runApp(new Center(child: new Text('This is an example')));

The runApp() function call is the key here. This function takes a given widget and attaches it to the screen. When called, the given widget will automatically fill the entire screen. There are other layout widgets that can be used to do other (such as the Align and Center widgets).

If you call the runApp() function again, then the given Widget will simply replace the previous root widget that was attached to the screen. This operation is carried out in an efficient manner as the new Widget tree is compared to the previous and the differences are applied.

Enjoy the other snippets in this publication! Leave a comment below or tweet me if with any questions / suggestions!

Leave a Reply

Your email address will not be published. Required fields are marked *