text
stringlengths
1
372
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,