arrow_back

Build a Two Screen Flutter Application

로그인 가입
700개 이상의 실습 및 과정 이용하기

Build a Two Screen Flutter Application

실습 1시간 universal_currency_alt 크레딧 1개 show_chart 입문
info 이 실습에는 학습을 지원하는 AI 도구가 통합되어 있을 수 있습니다.
700개 이상의 실습 및 과정 이용하기

GSP1010

Google Cloud self-paced labs logo

Overview

Flutter is Google's UI toolkit for building beautiful, natively compiled applications for mobile, web, and desktop from a single codebase. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source.

In this lab, you will create a Flutter app using generated template code. The basic Flutter interface provides a simple example to get started programming in Flutter.

Flutter interface

What you'll learn

  • How to write a Flutter app that looks natural on iOS, Android, and the web
  • Basic structure of a Flutter app
  • Using multiple pages
  • Using hot reload for a quicker development cycle

Prerequisites

Based on the content, it is recommended to have some familiarity with:

  • Flutter
  • Dart

Task 1. Open the Code Server Editor

In this lab, you will use a custom editor that includes the Flutter and Dart extensions. From the initial lab panel:

Lab Details pane with the IDE and teh Live Server addresses

  1. Copy the Flutter IDE address displayed
  2. Paste the IDE address into a Browser tab

Task 2. Creating a Flutter template

In this section, create a Flutter Web application called first_app.

  1. Click the hamburger button in the top left (Application menu) and then click Terminal > New Terminal.

  2. In the terminal, enter the following command:

flutter create first_app
  1. Move to the first_app directory:
cd first_app
  1. Close the terminal window:
exit

The first_app directory and template code have now been created.

Task 3. Opening the new app

In this section, explore the Flutter Web application called first_app.

  1. Open the folder first_app.
  2. Acknowledge the on-screen notifications.

At this point, you will have the Flutter application open in the current workspace.

Task 4. Running the Flutter web application

In this section, run the Flutter Web application from the command line.

  1. In the editor, open a Terminal.
  2. Ensure the terminal directory is set to first_app.
  3. Run the Flutter web server:
fwr
  1. The running web server should look similar to below:

Output indicating the web server is running

  1. Copy the Flutter Live Server address from the lab Panel.

  2. Paste the address into a new browser tab.

  3. The browser will render the web application e.g. Flutter Demo Home Page

Note: Rendering of the web application can take up to ten seconds. The view will show the application based on the code in the editor.

Feel free to interact with the running application.

Task 5. Designing the application

In this section, use widgets to build a basic two screen application.

Illustration of a basic two-screen application

From the above sketch, we note that our application uses the following types of widgets.

Widget Description
AppBar Header bar
Scaffold Screen layout
Text Text entry fields
ListView Item list

Task 6. Working with ListView

In this section to build a basic item list, the Flutter page will look similar to the image below:

My Awesome App list with seven items, January, February, March, April, May, June, and July

In the image, there is a list of months presented. A ListView widget is capable of listing items.

body: ListView( children: <Widget>[ ListTile( title: Text('January'), ), ListTile( title: Text('February'), ), ListTile( title: Text('March'), ), ], ),

To learn more, refer to Lists in the Flutter documentation. In the exercise below, create a basic short list.

  1. Begin by replacing the template code in main.dart with the code below:
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { final title = 'MyAwesome App'; return MaterialApp( title: title, home: Scaffold( appBar: AppBar( title: Text(title), ), body: ListView( children: <Widget>[ ListTile( title: Text('January'), ), ListTile( title: Text('February'), ), ListTile( title: Text('March'), ), ], ), ), ); } }
  1. Save the updated code.

  2. Click in the open Terminal window, and press ‘r'.

  3. Switch to the Flutter Device browser tab.

  4. Press CTRL+R to reload the page.

The code in the above example creates a basic list. Similar to our application sketch, this presents data as outlined in our design.

Task 7. Managing ListView data

In this section, add user interaction to respond when a list item is selected.

Previously, we have looked at using a small amount of data being added to a ListView. Working with larger volumes of data can be challenging. To help with this situation, consider the ListView.builder.

  1. Declare a List:
final List<String> items = ['January', 'February', 'March'];
  1. ListView.builder declaration:
body: ListView.builder( itemCount: items.length, itemBuilder: (context, index) { return ListTile( title: Text('${items[index]}'), ); }, ),

In this exercise below, create a long list using a ListView.builder.

  1. Replace the MyApp class with the code below:
class MyApp extends StatelessWidget { final List<String> items = ['January', 'February', 'March']; @override Widget build(BuildContext context) { final title = 'MyAwesome App'; return MaterialApp( title: title, home: Scaffold( appBar: AppBar( title: Text(title), ), body: ListView.builder( itemCount: items.length, itemBuilder: (context, index) { return ListTile( title: Text('${items[index]}'), ); }, ), ), ); } }
  1. Click in the open Terminal window, and press ‘r'.

  2. Switch to the Flutter Device browser tab.

  3. Press CTRL+R to reload the page.

The code in this example creates a long list using the ListView.builder. The page rendered is exactly the same as before. However, this is a better approach to handling larger data sources.

Task 8. Adding interactivity

In this section, add user interaction to respond when a list item is selected. Add interactivity in Flutter typically requires a Gesture Detector. The type of gesture to capture is called a Tap.

Additionally, add a SnackBar to the code so there is a visual notification when a user taps on a list item.

onTap: () { final snackBar = SnackBar(content: Text('You selected $items[index]')); ScaffoldMessenger.of(context).showSnackBar(snackBar); },

In this exercise, the MyApp class with the following code:

  1. Update MyApp with the following code:
class MyApp extends StatelessWidget { final List<String> items = ['January', 'February', 'March']; @override Widget build(BuildContext context) { final title = 'MyAwesome App'; return MaterialApp( title: title, home: Scaffold( appBar: AppBar( title: Text(title), ), body: ListView.builder( itemCount: items.length, itemBuilder: (context, index) { return ListTile( title: Text('${items[index]}'), // When the child is tapped, show a snackbar. onTap: () { final snackBar = SnackBar(content: Text('You selected $items[index]')); ScaffoldMessenger.of(context).showSnackBar(snackBar); }, ); }, ), ), ); } } Note: The onTap resides in the ListTile widget as an accepted parameter. Tapping on a list item will then register as a tap and invoke the SnackBar.

The implementation of the SnackBar is temporary to provide feedback that our onTap is working correctly.

  1. Click in the open Terminal window, and press ‘r'.

  2. Switch to the Flutter Device browser tab.

  3. Press CTRL+R to reload the page.

Note: The onTap also provides a visual cue to indicate an action is available for the list item.

Hover over each item to see the mouse pointer change.

Task 9. Creating a details page

In this section, create a second Flutter page and display some more information.

Details page illustration

As the second page is very similar to the first page, this can help us construct the code.

  • In this exercise, add the following functionality to the main.dart file:
class MyDetails extends StatelessWidget { @override Widget build(BuildContext context) { final title = 'Details Page'; return Scaffold( appBar: AppBar( title: Text(title), ), body: Text('You selected January'), ); } } Note: The widget returned is a Scaffold. In the application there should be only one MaterialApp defined. Now the MaterialApp widget will be able to handle navigation properly in the application.

At this point, the Details Page is not connected to the first page. You'll correct that in the next section.

Task 10. Navigating between pages

In this section, enable navigation between the first page and the second:

Navigator.push( context, MaterialPageRoute(builder: (context) => MyDetails()), );

Previously, we enabled a SnackBar to indicate that an onTap event had occurred. To achieve navigation will require the following activities:

  1. Add a Navigator.push to MyApp:
class MyApp extends StatelessWidget { final List<String> items = ['January', 'February', 'March']; @override Widget build(BuildContext context) { final title = 'MyAwesome App'; return MaterialApp( title: title, home: Scaffold( appBar: AppBar( title: Text(title), ), body: ListView.builder( itemCount: items.length, itemBuilder: (context, index) { return ListTile( title: Text('${items[index]}'), // When the child is tapped, show a snackbar. onTap: () { Navigator.push( context, MaterialPageRoute(builder: (context) => MyDetails()), ); }, ); }, ), ), ); } }
  1. Click in the open Terminal window, and press ‘r'.

  2. Switch to the Flutter Device browser tab.

  3. Press CTRL+R to reload the page.

Illustration of basic two screen application

Now, the application will be able to move between the Main page and the Details page. However, the details page always says January. You'll fix that in the next section.

Task 11. Passing data between pages

In this section, learn how to pass information between pages.

  • Update the navigation code to pass a parameter:
Navigator.push( context, MaterialPageRoute(builder: (context) => MyDetails(items[index])), );

MyApp

In the code below, use MaterialPageRoute to communicate with your new page. To pass information to the Details page, we use the list item string as a parameter.

  • Update MyApp to call MyDetails with a parameter:
class MyApp extends StatelessWidget { final List<String> items = ['January', 'February', 'March']; @override Widget build(BuildContext context) { final title = 'MyAwesome App'; return MaterialApp( title: title, home: Scaffold( appBar: AppBar( title: Text(title), ), body: ListView.builder( itemCount: items.length, itemBuilder: (context, index) { return ListTile( title: Text('${items[index]}'), // When the child is tapped, show a snackbar. onTap: () { Navigator.push( context, MaterialPageRoute(builder: (context) => MyDetails(items[index])), ); }, ); }, ), ), ); } }

MyDetails

Now that MyApp expects to pass data to the MyDetails class. Make the following updates to accept a parameter using a Constructor.

  1. A Constructor is used to accept the parameter i.e. the list item.
MyDetails(this.month);
  1. A variable is used to hold the information passed as a parameter.
final String month;
  1. The Text widget is used to display the information passed.
body: Text('You selected $month'),

To make the changes, follow the steps below:

  1. Update MyDetails to accept a parameter:
class MyDetails extends StatelessWidget { final String month; MyDetails(this.month); @override Widget build(BuildContext context) { final title = 'Details Page'; return Scaffold( appBar: AppBar( title: Text(title), ), body: Text('You selected $month'), ); } }
  1. Click in the open Terminal window, and press ‘r'.

  2. Switch to the Flutter Device browser tab.

  3. Press CTRL+R to reload the page:

Details Page that displays: You selected January

Note: With the string parameter available in MyDetails, the Text widget can use the variable month and display it to the user.

Click Check my progress to verify the objective. Assess my progress

Congratulations!

You have successfully completed the lab and demonstrated your knowledge of Flutter. Over the course of this lab, you have performed the following tasks:

  • Used a ListView widget
  • Refactored code to use ListView.builder
  • Added notifications to Flutter using a SnackBar widget
  • Declared a new Flutter page
  • Passed data between Flutter pages

Manual Last Updated November 15, 2024

Lab Last Tested November 15, 2024

Copyright 2025 Google LLC. All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

시작하기 전에

  1. 실습에서는 정해진 기간 동안 Google Cloud 프로젝트와 리소스를 만듭니다.
  2. 실습에는 시간 제한이 있으며 일시중지 기능이 없습니다. 실습을 종료하면 처음부터 다시 시작해야 합니다.
  3. 화면 왼쪽 상단에서 실습 시작을 클릭하여 시작합니다.

시크릿 브라우징 사용

  1. 실습에 입력한 사용자 이름비밀번호를 복사합니다.
  2. 비공개 모드에서 콘솔 열기를 클릭합니다.

콘솔에 로그인

    실습 사용자 인증 정보를 사용하여
  1. 로그인합니다. 다른 사용자 인증 정보를 사용하면 오류가 발생하거나 요금이 부과될 수 있습니다.
  2. 약관에 동의하고 리소스 복구 페이지를 건너뜁니다.
  3. 실습을 완료했거나 다시 시작하려고 하는 경우가 아니면 실습 종료를 클릭하지 마세요. 이 버튼을 클릭하면 작업 내용이 지워지고 프로젝트가 삭제됩니다.

현재 이 콘텐츠를 이용할 수 없습니다

이용할 수 있게 되면 이메일로 알려드리겠습니다.

감사합니다

이용할 수 있게 되면 이메일로 알려드리겠습니다.

한 번에 실습 1개만 가능

모든 기존 실습을 종료하고 이 실습을 시작할지 확인하세요.

시크릿 브라우징을 사용하여 실습 실행하기

이 실습을 실행하려면 시크릿 모드 또는 시크릿 브라우저 창을 사용하세요. 개인 계정과 학생 계정 간의 충돌로 개인 계정에 추가 요금이 발생하는 일을 방지해 줍니다.