text
stringlengths
1
474
Flutter comes with a beautiful, built-in implementation of Material Design,
which handles much of the styling and theming needs
that you would typically do.Xamarin.Forms does have a global ResourceDictionary
where you can share styles across your app.
Alternatively, there is Theme support currently in preview.In Flutter, you declare themes in the top level widget.To take full advantage of Material Components in your app,
you can declare a top level widget MaterialApp
as the entry point to your application.
MaterialApp is a convenience widget
that wraps a number of widgets that are commonly required
for applications implementing Material Design.
It builds upon a WidgetsApp by adding Material-specific functionality.You can also use a WidgetsApp as your app widget,
which provides some of the same functionality,
but is not as rich as MaterialApp.To customize the colors and styles of any child components,
pass a ThemeData object to the MaterialApp widget.
For example, in the following code,
the color scheme from seed is set to deepPurple and text selection color is red.
<code_start>class SampleApp extends StatelessWidget {
/// This widget is the root of your application.
const SampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sample App',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
textSelectionTheme:
const TextSelectionThemeData(selectionColor: Colors.red),
),
home: const SampleAppPage(),
);
}
}<code_end>
<topic_end>
<topic_start>
Databases and local storage
<topic_end>
<topic_start>
How do I access shared preferences or UserDefaults?
Xamarin.Forms developers will likely be familiar with the
Xam.Plugins.Settings plugin.In Flutter, access equivalent functionality using the
shared_preferences plugin. This plugin wraps the
functionality of both UserDefaults and the Android
equivalent, SharedPreferences.<topic_end>
<topic_start>
How do I access SQLite in Flutter?
In Xamarin.Forms most applications would use the sqlite-net-pcl
plugin to access SQLite databases.In Flutter, on macOS, Android, and iOS,
access this functionality using the
sqflite plugin.<topic_end>
<topic_start>
Debugging
<topic_end>
<topic_start>
What tools can I use to debug my app in Flutter?
Use the DevTools suite for debugging Flutter or Dart apps.DevTools includes support for profiling, examining the heap,
inspecting the widget tree, logging diagnostics, debugging,
observing executed lines of code,
debugging memory leaks and memory fragmentation.
For more information, see the DevTools documentation.<topic_end>
<topic_start>
Notifications
<topic_end>
<topic_start>
How do I set up push notifications?
In Android, you use Firebase Cloud Messaging to set up
push notifications for your app.In Flutter, access this functionality using the
firebase_messaging plugin.
For more information on using the Firebase Cloud Messaging API, see the
firebase_messaging plugin documentation.
<topic_end>
<topic_start>Introduction to declarative UI
This introduction describes the conceptual difference between the
declarative style used by Flutter, and the imperative style used by
many other UI frameworks.<topic_end>
<topic_start>
Why a declarative UI?
Frameworks from Win32 to web to Android and iOS typically use an imperative
style of UI programming. This might be the style you’re most familiar
with—where you manually construct a full-functioned UI entity,
such as a UIView or equivalent, and later mutate it using methods and
setters when the UI changes.In order to lighten the burden on developers from having to program how to
transition between various UI states, Flutter, by contrast,
lets the developer describe the current UI state and leaves the
transitioning to the framework.This, however, requires a slight shift in thinking for how to manipulate UI.<topic_end>
<topic_start>
How to change UI in a declarative framework
Consider a simplified example below:In the imperative style, you would typically go to ViewB’s owner
and retrieve the instance b using selectors or with findViewById or similar,
and invoke mutations on it (and implicitly invalidate it). For example:You might also need to replicate this configuration in the constructor of
ViewB since the source of truth for the UI might outlive instance b itself.In the declarative style, view configurations (such as Flutter’s Widgets)
are immutable and are only lightweight “blueprints”. To change the UI,
a widget triggers a rebuild on itself (most commonly by calling setState()
on StatefulWidgets in Flutter) and constructs a new Widget subtree.
<code_start>// Declarative style
return ViewB(
color: red,
child: const ViewC(),
);<code_end>
Here, rather than mutating an old instance b when the UI changes,
Flutter constructs new Widget instances. The framework manages many of the