22 Flutter
22 Flutter
The Flutter
Framework
Mobile Computing
APM@FEUP 1
What is Flutter
❖ Flutter is an open-source application
development kit created by Google
• Ultimately it aims applications for
▪ Mobile (Android and iOS)
▪ Web (deployed in servers, embedded in browsers, PWA) (beta)
▪ Desktop (still in alpha)
▪ Windows (needs VS 2019)
▪ MacOS (needs XCode)
▪ Linux (needs CLang, CMake, GTK)
▪ The experimental Google OS Fuchsia
• It uses the Dart programming language and runtime
• Tools, Resources and Installation from
▪ https://flutter.dev
Mobile Computing
APM@FEUP 2
Mobile Application Approaches
Mobile Computing
APM@FEUP 3
Web Applications for Mobile
❖ Same technologies as other web applications
• Run on the device browser and a remote server
+ RhoMobile
(Tau technologies)
Mobile Computing
APM@FEUP 5
Near Native (Hybrid-native) technologies
❖ Produce and execute on native (or VM) code
• UI near the native, facilities to access the original API
▪ With good performance
JSX, JS C#
. web like separated UI specification . architecture pattern oriented (MVVM)
. separated UI specification
. Xamarin.Forms rendered with
Android / iOS native views
Dart
. widgets Mobile Computing
APM@FEUP 6
Flutter Development Tools
❖ You can install your development tools
• Windows, MacOS, or Linux
▪ Flutter SDK, Dart SDK, Android and/or iOS SDK
▪ CLI tools
• With plugins, high level IDEs
Other functionalities need calling and data exchange with native services
that is done using Flutter platform channels
Mobile Computing
APM@FEUP 9
Flutter Side App and Widgets
❖ Flutter apps start from the Dart main() function
• A call to the Framework runApp(…) should be made
▪ The parameter should be a widget derived object, with the UI
of the app’s home page
import ‘package:flutter/… ‘; // needed Dart and Flutter imports
Mobile Computing
APM@FEUP 10
Widgets
❖ All the UI is made by a tree of Widgets
• Not only displayed objects, but also almost anything
related to a UI is a Widget (e.g. the GestureDetector or
the Align widget)
▪ Almost all widgets are a StatelessWidget or a StatefulWidget
Mobile Computing
APM@FEUP 11
Stateless Widgets
❖ Stateless widgets are immutable
• They receive configuration data through the
constructor
▪ The constructor calls the build(…) method that returns the
widget object
▪ The build(…) cannot be called again
• To redraw a StatelessWidget a new instance must be
created
Mobile Computing
APM@FEUP 12
Stateless Example
...
composition
pattern
Mobile Computing
APM@FEUP 13
Stateful Widgets
❖ Have an associated state object
• The state object is mutable and redraws the
immutable widget through its build() method
▪ The StatefulWidget derived class should override at least the
createState() method, that returns the associated state object
▪ The associated State class should derive the build() method
that returns the Widget (created the first time or redrawn)
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
@override
Widget build(BuildContext context) {
return Container(
… // the UI of this widget
);
}
}
Mobile Computing
APM@FEUP 14
Stateful Lyfecycle
State object
It calls setState() on
_MyHomePageState
changing the property
value.
Mobile Computing
APM@FEUP 16
Some Generic Layout Widgets
The Widgets Flutter Framework library contains 935 classes and 3 exception types
Some represent the top-level app like MaterialApp or CupertinoApp and some others
the common structure of a page, like Scaffold
To organize the page (screen) widget tree there are many other widgets that can be used,
with one or multiple children.
Layout a list of
Column Row children in vertical
or horizontal direction
Top structure
Mobile Computing
APM@FEUP 18
Some Other Widgets
Mobile Computing
APM@FEUP 19
Page Navigation
The Navigator widget allows the replacement of a page by another using a stack discipline.
It is possible to create a set of navigation routes previously in the app, or build it when
we want to navigate to it.
void main() {
runApp(MaterialApp(
home: MyAppHome(), // becomes the route named '/'
routes: <String, WidgetBuilder> {
'/a': (BuildContext context) => MyPage(title: 'page A'),
'/b': (BuildContext context) => MyPage(title: 'page B'),
'/c': (BuildContext context) => MyPage(title: 'page C'),
},
));
} // within the MyAppHome widget
…
Or Navigator.of(context).push(MaterialPageRoute(
// within the MyAppHome widget builder: (context) => MyPage(title: ‘page B’) )
… );
Navigator.of(context).pushNamed('/b');
Mobile Computing
APM@FEUP 20
Platform Specific Code
It is possible to call any native
functionality using MethodChanels
Mobile Computing
APM@FEUP 21
Example
Mobile Computing
APM@FEUP 22
App Architecture
. There are some difficulties in managing State and Business Logic (Domain Logic,
linked to data, and Application Logic, linked to functionalities/user operations) in
Flutter applications.
. It is not easy to adapt well known architectures, like MVC, to the Flutter app structure.
. Many architectural patterns have been proposed with corresponding plugins …
(https://flutter.dev/docs/development/data-and-backend/state-mgmt/options)
. One of them leverages the Flutter class ChangeNotifier (notifies if data changes) and
the package Provider (binds Models to the BuildContext) to implement a clean
MVC+S architecture.
(https://blog.gskinner.com/archives/2020/09/flutter-state-management-with-mvcs.html)
Provider package:
(https://pub.dev/packages/provider)
Mobile Computing
APM@FEUP 23