text
stringlengths 1
474
|
---|
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.
|
@override
|
Widget build(BuildContext context) {
|
return MaterialApp(
|
title: 'Sample App',
|
theme: ThemeData(
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
),
|
home: const SampleAppPage(),
|
);
|
}
|
}
|
class SampleAppPage extends StatefulWidget {
|
const SampleAppPage({super.key});
|
@override
|
State<SampleAppPage> createState() => _SampleAppPageState();
|
}
|
class _SampleAppPageState extends State<SampleAppPage> {
|
// Default placeholder text.
|
String textToShow = 'I Like Flutter';
|
void _updateText() {
|
setState(() {
|
// Update the text.
|
textToShow = 'Flutter is Awesome!';
|
});
|
}
|
@override
|
Widget build(BuildContext context) {
|
return Scaffold(
|
appBar: AppBar(
|
title: const Text('Sample App'),
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.