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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s