text
stringlengths
1
474
An excerpt displays the initial line(s) of text in a paragraph,
and handles the overflow text, often using an ellipsis.In Flutter, use the maxLines property of a Text widget
to specify the number of lines to include in the excerpt,
and the overflow property for handling overflow text.
<topic_end>
<topic_start>Flutter for Xamarin.Forms developers
This document is meant for Xamarin.Forms developers
looking to apply their existing knowledge
to build mobile apps with Flutter.
If you understand the fundamentals of the Xamarin.Forms framework,
then you can use this document as a jump start to Flutter development.Your Android and iOS knowledge and skill set
are valuable when building with Flutter,
because Flutter relies on the native operating system configurations,
similar to how you would configure your native Xamarin.Forms projects.
The Flutter Frameworks is also similar to how you create a single UI,
that is used on multiple platforms.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>
Project setup
<topic_end>
<topic_start>
How does the app start?
For each platform in Xamarin.Forms,
you call the LoadApplication method,
which creates a new application and starts your app.In Flutter, the default main entry point is
main where you load your Flutter app.
<code_start>void main() {
runApp(const MyApp());
}<code_end>
In Xamarin.Forms, you assign a Page to the
MainPage property in the Application class.In Flutter, “everything is a widget”, even the application itself.
The following example shows MyApp, a simple application Widget.
<code_start>class MyApp extends StatelessWidget {
/// This widget is the root of your application.
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: Text(
'Hello World!',
textDirection: TextDirection.ltr,
),
);
}
}<code_end>
<topic_end>
<topic_start>
How do you create a page?
Xamarin.Forms has many types of pages;
ContentPage is the most common.
In Flutter, you specify an application widget that holds your root page.
You can use a MaterialApp widget, which supports Material Design,
or you can use a CupertinoApp widget, which supports an iOS-style app,
or you can use the lower level WidgetsApp,
which you can customize in any way you want.The following code defines the home page, a stateful widget.
In Flutter, all widgets are immutable,
but two types of widgets are supported: Stateful and Stateless.
Examples of a stateless widget are titles, icons, or images.The following example uses MaterialApp,
which holds its root page in the home property.
<code_start>class MyApp extends StatelessWidget {
/// This widget is the root of your application.
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}<code_end>
From here, your actual first page is another Widget,
in which you create your state.A Stateful widget, such as MyHomePage below, consists of two parts.
The first part, which is itself immutable, creates a State object
that holds the state of the object. The State object persists over
the life of the widget.
<code_start>class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}<code_end>
The State object implements the build() method for the stateful widget.When the state of the widget tree changes, call setState(),
which triggers a build of that portion of the UI.
Make sure to call setState() only when necessary,
and only on the part of the widget tree that has changed,
or it can result in poor UI performance.
<code_start>class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Take the value from the MyHomePage object that was created by
// the App.build method, and use it to set the appbar title.
title: Text(widget.title),