3

How To Get Started with Your First Flutter App

 3 years ago
source link: https://www.digitalocean.com/community/tutorials/flutter-your-first-flutter-app
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Tutorial

How To Get Started with Your First Flutter App

Flutter

Introduction

Flutter is an Open Source framework created by Google that focuses on the creation of cross-platform applications. Flutter primarily targets iOS and Android, but is increasingly adding support for desktop platforms, too.

Note: Flutter apps are built using the Dart programming language. If you’re new to Dart, you may want to start by getting a general overview of the language first.

In this article, you will create your first Flutter application and explore the generated code.

Prerequisites

To complete this tutorial, you will need:

  • To download and install Flutter.
  • To download and install Android Studio or Visual Studio Code. Android Studio offers an integrated, feature-rich IDE with support for Flutter. Visual Studio Code offers more lightweight, but functional support.
  • It is recommended to install plugins for your code editor:

    • Flutter and Dart plugins installed for Android Studio.
    • Flutter extension installed for Visual Studio Code.

This article was originally written using Flutter 1.2.x. It has been updated to support Flutter 1.22.2.

Step 1 — Creating a New Flutter Project

Once you have installed Flutter and have the appropriate dependencies (Android SDK or XCode depending on your machine) installed, you can now create a new Flutter project.

First, open your terminal window, navigate to the directory where you want to start your project, and run the following command:

flutter create hello_flutter  

Next, change into the project directory:

cd hello_flutter  

Then, open this project with your code editor of choice.

Step 2 — Launching the Project

Consult the documentation for running the code in Visual Studio Code or Android Studio.

For example, with Visual Studio Code, open the Run and Debug:

Screenshot of running the application in Visual Studio Code.

Then, select Dart & Flutter from the dropdown and then choose the hello_flutter configuration. Specify the simulator (either web, iOS, or Android) in the status bar. And run the application by pressing Start Debugging (that resembles a “Play button”).

You will then observe the demo application in the simulator or browser:

Screenshot of the application in the iOS simulator.

Android Studio will require you to specify a Device and select a configuration.

Step 3 — Exploring the Code

You’ve created your first Flutter application! And ran it in an emulator! In this section, you will read some of the code.

Note: The starter code generated by flutter is part of the official Flutter codebase, and available under the following license.

Now, open lib/main.dart in your code editor.

MyApp

The first part of the file defines MyApp. This class extends StatelessWidget:

lib/main.dart
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

// ...
 

We’re firstly importing the Material package from Flutter. This is what gives our application the Material Design look and subsequent access to Material style widgets and functionality.

Then, we’re registering the MyApp widget as the main widget for our application using the runApp method.

Inside of MyApp, we’re returning a build method of type Widget which returns a MaterialApp. The MaterialApp holds metadata such as current ThemeData, the application title, the current home route, and so on.

Note: We don’t have to use MaterialApp here, we could also use the iOS-styled CupertinoApp or a custom style with WidgetsApp.

MyHomePage

The next part of the file defines MyHomePage. This class extends StatefulWidget:

lib/main.dart
// ...

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

// ...
 

The last part of this file defines _MyHomePageState. This class extends State for that widget and the build method. If you’ve ever used React before, this is similar to the render method of JSX.

One of the more important things to consider with the above example is the fact that we’re overriding the createState method to provide our own way of managing state:

lib/main.dart
// ...

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
 

The _counter state can therefore be changed with setState(). Next, we define the build method which creates a Scaffold for our application that contains an appBar and a body.

The Scaffold class can be thought of as a top-level container when using MaterialApp. This allows us to easily add navigation bars, floating action buttons, drawers, avoid notches, and much more.

Whenever we call setState(), the widget’s build method is also called, thus, updating the view and redrawing with the new state. In our example, you can see that we’re making this call to setState within the FloatingActionButton via the onPressed: _incrementCounter function.

Conclusion

In this article, you created your first Flutter application and explored the generated code.

Take the opportunity to experiment with the application and change values for the counter and text.

Continue your learning, by checking out our Flutter topic page for exercises and programming projects.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK