text
stringlengths
1
474
}
}<code_end>
For more information, see the documentation for async and await.<topic_end>
<topic_start>
The basics
<topic_end>
<topic_start>
How do I create a Flutter app?
To create an app using React Native,
you would run create-react-native-app from the command line.To create an app in Flutter, do one of the following:For more information, see Getting started, which
walks you through creating a button-click counter app.
Creating a Flutter project builds all the files that you
need to run a sample app on both Android and iOS devices.<topic_end>
<topic_start>
How do I run my app?
In React Native, you would run npm run or yarn run from the project
directory.You can run Flutter apps in a couple of ways:Your app runs on a connected device, the iOS simulator,
or the Android emulator.For more information, see the Flutter Getting started
documentation.<topic_end>
<topic_start>
How do I import widgets?
In React Native, you need to import each required component.In Flutter, to use widgets from the Material Design library,
import the material.dart package. To use iOS style widgets,
import the Cupertino library. To use a more basic widget set,
import the Widgets library.
Or, you can write your own widget library and import that.
<code_start>import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:my_widgets/my_widgets.dart';<code_end>
Whichever widget package you import,
Dart pulls in only the widgets that are used in your app.For more information, see the Flutter Widget Catalog.<topic_end>
<topic_start>
What is the equivalent of the React Native “Hello world!” app in Flutter?
In React Native, the HelloWorldApp class extends React.Component and
implements the render method by returning a view component.In Flutter, you can create an identical “Hello world!” app using the
Center and Text widgets from the core widget library.
The Center widget becomes the root of the widget tree and has one child,
the Text widget.
<code_start>// Flutter
import 'package:flutter/material.dart';
void main() {
runApp(
const Center(
child: Text(
'Hello, world!',
textDirection: TextDirection.ltr,
),
),
);
}<code_end>
The following images show the Android and iOS UI for the basic Flutter
“Hello world!” app.Now that you’ve seen the most basic Flutter app, the next section shows how to
take advantage of Flutter’s rich widget libraries to create a modern, polished
app.<topic_end>
<topic_start>
How do I use widgets and nest them to form a widget tree?
In Flutter, almost everything is a widget.Widgets are the basic building blocks of an app’s user interface.
You compose widgets into a hierarchy, called a widget tree.
Each widget nests inside a parent widget
and inherits properties from its parent.
Even the application object itself is a widget.
There is no separate “application” object.
Instead, the root widget serves this role.A widget can define:The following example shows the “Hello world!” app using widgets from the
Material library. In this example, the widget tree is nested inside the
MaterialApp root widget.
<code_start>// Flutter
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welcome to Flutter',
home: Scaffold(
appBar: AppBar(
title: const Text('Welcome to Flutter'),
),
body: const Center(
child: Text('Hello world'),
),
),
);
}
}<code_end>
The following images show “Hello world!” built from Material Design widgets.
You get more functionality for free than in the basic “Hello world!” app.When writing an app, you’ll use two types of widgets:
StatelessWidget or StatefulWidget.
A StatelessWidget is just what it sounds like—a
widget with no state. A StatelessWidget is created once,
and never changes its appearance.
A StatefulWidget dynamically changes state based on data
received, or user input.The important difference between stateless and stateful
widgets is that StatefulWidgets have a State object
that stores state data and carries it over
across tree rebuilds, so it’s not lost.In simple or basic apps it’s easy to nest widgets,
but as the code base gets larger and the app becomes complex,
you should break deeply nested widgets into
functions that return the widget or smaller classes.