Show Tip: This codelab walks you through writing your first Flutter app. You might prefer to try writing your first Flutter app on the web. If you prefer an instructor-led version of this codelab, check out the following workshop: This is a guide to creating your first Flutter app. If you are familiar with object-oriented code and basic programming concepts such as variables, loops, and conditionals, you can complete this tutorial. You don’t need previous experience with Dart, mobile, desktop, or web programming. This codelab is part 1 of a two-part codelab. You can find part 2 on Google Developers Codelabs (as well as a copy of this codelab, part 1). What you’ll build in part 1You’ll implement a simple app that generates proposed names for a startup company. The user can select and unselect names, saving the best ones. The code lazily generates 10 names at a time. As the user scrolls, more names are generated. There is no limit to how far a user can scroll. The animated GIF shows how the app works at the completion of part 1.
In part 2 of this codelab, you’ll add interactivity, modify the app’s theme, and add the ability to navigate to a new screen (called a route in Flutter). You need two pieces of software to complete this lab: the Flutter SDK and an editor. This codelab assumes Android Studio, but you can use your preferred editor. You can run this codelab by using any of the following devices:
Every Flutter app you create also compiles for the web. In your IDE under the devices pulldown, or at the command line using flutter devices, you should now see Chrome and Web server listed. The Chrome device automatically starts Chrome. The Web server starts a server that hosts the app so that you can load it from any browser. Use the Chrome device during development so that you can use DevTools, and the web server when you want to test on other browsers. For more information, see Building a web application with Flutter and Write your first Flutter app on the web. Also, Flutter apps can compile for desktop. You should see your operating system listed in your IDE under devices, for example: Windows (desktop), or at the command line using flutter devices. For more information on building apps for desktop, see Write a Flutter desktop application. Step 1: Create the starter Flutter appCreate a simple, templated Flutter app, using the instructions in Getting Started with your first Flutter app. Name the project startup_namer (instead of flutter_app). Tip: If you don’t see “New Flutter Project” as an option in your IDE, make sure you have the plugins installed for Flutter and Dart. You’ll mostly edit lib/main.dart, where the Dart code lives.
Observations
Step 2: Use an external packageIn this step, you’ll start using an open-source package named english_words, which contains a few thousand of the most used English words plus some utility functions. You can find the english_words package, as well as many other open source packages, on pub.dev.
Problems?If your app is not running correctly, look for typos. If you want to try some of Flutter’s debugging tools, check out the DevTools suite of debugging and profiling tools. If needed, use the code at the following links to get back on track.
Stateless widgets are immutable, meaning that their properties can’t change—all values are final. Stateful widgets maintain state that might change during the lifetime of the widget. Implementing a stateful widget requires at least two classes: 1) a StatefulWidget class that creates an instance of 2) a State class. The StatefulWidget class is, itself, immutable and can be thrown away and regenerated, but the State class persists over the lifetime of the widget. In this step, you’ll add a stateful widget, RandomWords, which creates its State class, _RandomWordsState. You’ll then use RandomWords as a child inside the existing MyApp stateless widget.
Tip: If you see a warning on a hot reload that you might need to restart the app, consider restarting it. The warning might be a false positive, but restarting your app ensures that your changes are reflected in the app’s UI. If your app is not running correctly, look for typos. If you want to try some of Flutter’s debugging tools, check out the DevTools suite of debugging and profiling tools. If needed, use the code at the following link to get back on track. In this step, you’ll expand _RandomWordsState to generate and display a list of word pairings. As the user scrolls the list (displayed in a ListView widget) grows infinitely. ListView’s builder factory constructor allows you to build a list view lazily, on demand.
Problems?If your app is not running correctly, look for typos. If you want to try some of Flutter’s debugging tools, check out the DevTools suite of debugging and profiling tools. If needed, use the code at the following link to get back on track. Profile or release runsImportant: Do not test the performance of your app with debug and hot reload enabled. So far you’ve been running your app in debug mode. Debug mode trades performance for useful developer features such as hot reload and step debugging. It’s not unexpected to see slow performance and janky animations in debug mode. Once you are ready to analyze performance or release your app, you’ll want to use Flutter’s “profile” or “release” build modes. For more details, see Flutter’s build modes. Important: If you’re concerned about the package size of your app, see Measuring your app’s size. Next stepsThe app from part 2 Congratulations! You’ve written an interactive Flutter app that runs on iOS, Android, Windows and web. In this codelab, you’ve:
If you would like to extend this app, proceed to part 2 on the Google Developers Codelabs site, where you add the following functionality:
|