text
stringlengths
1
80
<topic_start>
flutter for android developers
this document is meant for android developers looking to apply their
existing android knowledge to build mobile apps with flutter.
if you understand the fundamentals of the android framework then you
can use this document as a jump start to flutter development.
info note
to integrate flutter code into your android app, see
add flutter to existing app.
your android knowledge and skill set are highly valuable when building with
flutter, because flutter relies on the mobile operating system for numerous
capabilities and configurations. flutter is a new way to build UIs for mobile,
but it has a plugin system to communicate with android (and iOS) for non-UI
tasks. if you’re an expert with android, you don’t have to relearn everything
to use flutter.
this document can be used as a cookbook by jumping around and
finding questions that are most relevant to your needs.
<topic_end>
<topic_start>
views
<topic_end>
<topic_start>
what is the equivalent of a view in flutter?
how is react-style, or declarative, programming different than the
traditional imperative style?
for a comparison, see introduction to declarative UI.
in android, the view is the foundation of everything that shows up on the
screen. buttons, toolbars, and inputs, everything is a view.
in flutter, the rough equivalent to a view is a widget.
widgets don’t map exactly to android views, but while you’re getting
acquainted with how flutter works you can think of them as
“the way you declare and construct UI”.
however, these have a few differences to a view. to start, widgets have a
different lifespan: they are immutable and only exist until they need to be
changed. whenever widgets or their state change, flutter’s framework creates
a new tree of widget instances. in comparison, an android view is drawn once
and does not redraw until invalidate is called.
flutter’s widgets are lightweight, in part due to their immutability.
because they aren’t views themselves, and aren’t directly drawing anything,
but rather are a description of the UI and its semantics that get “inflated”
into actual view objects under the hood.
flutter includes the material components library.
these are widgets that implement the
material design guidelines. material design is a
flexible design system optimized for all platforms,
including iOS.
but flutter is flexible and expressive enough to implement any design language.
for example, on iOS, you can use the cupertino widgets
to produce an interface that looks like apple’s iOS design language.
<topic_end>
<topic_start>
how do i update widgets?
in android, you update your views by directly mutating them. however,
in flutter, widgets are immutable and are not updated directly,
instead you have to work with the widget’s state.
this is where the concept of stateful and stateless widgets comes from.
a StatelessWidget is just what it sounds like—a
widget with no state information.
StatelessWidgets are useful when the part of the user interface
you are describing does not depend on anything other than the configuration
information in the object.
for example, in android, this is similar to placing an ImageView
with your logo. the logo is not going to change during runtime,
so use a StatelessWidget in flutter.
if you want to dynamically change the UI based on data received
after making an HTTP call or user interaction then you have to work
with StatefulWidget and tell the flutter framework that the widget’s
state has been updated so it can update that widget.
the important thing to note here is at the core both stateless and stateful
widgets behave the same. they rebuild every frame, the difference is the
StatefulWidget has a state object that stores state data across frames
and restores it.
if you are in doubt, then always remember this rule: if a widget changes
(because of user interactions, for example) it’s stateful.
however, if a widget reacts to change, the containing parent widget can
still be stateless if it doesn’t itself react to change.
the following example shows how to use a StatelessWidget. a common
StatelessWidget is the text widget. if you look at the implementation of
the text widget you’ll find that it subclasses StatelessWidget.
<code_start>
text(
'i like flutter!',
style: TextStyle(fontWeight: FontWeight.bold),
);
<code_end>
as you can see, the text widget has no state information associated with it,
it renders what is passed in its constructors and nothing more.
but, what if you want to make “i like flutter” change dynamically, for
example when clicking a FloatingActionButton?
to achieve this, wrap the text widget in a StatefulWidget and
update it when the user clicks the button.
for example:
<code_start>
import 'package:flutter/material.dart';
void main() {
runApp(const SampleApp());
}
class SampleApp extends StatelessWidget {
const SampleApp({super.key});
// this widget is the root of your application.

No dataset card yet

New: Create and edit this dataset card directly on the website!

Contribute a Dataset Card
Downloads last month
0
Add dataset card