text stringlengths 1 474 |
|---|
user interface it represents. |
It’s common to begin your app with a WidgetApp class, |
like CupertinoApp. |
<code_start>class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
@override |
Widget build(BuildContext context) { |
// Returns a CupertinoApp that, by default, |
// has the look and feel of an iOS app. |
return const CupertinoApp( |
home: HomePage(), |
); |
} |
}<code_end> |
The widget used in HomePage might begin with the Scaffold class. |
Scaffold implements a basic layout structure for an app. |
<code_start>class HomePage extends StatelessWidget { |
const HomePage({super.key}); |
@override |
Widget build(BuildContext context) { |
return const Scaffold( |
body: Center( |
child: Text( |
'Hello, World!', |
), |
), |
); |
} |
}<code_end> |
Note how Flutter uses the Center widget. |
SwiftUI renders a view’s contents in its center by default. |
That’s not always the case with Flutter. |
Scaffold doesn’t render its body widget at the center of the screen. |
To center the text, wrap it in a Center widget. |
To learn about different widgets and their default behaviors, check out |
the Widget catalog.<topic_end> |
<topic_start> |
Adding Buttons |
In SwiftUI, you use the Button struct to create a button.To achieve the same result in Flutter, |
use the CupertinoButton class: |
<code_start> CupertinoButton( |
onPressed: () { |
// This closure is called when your button is tapped. |
}, |
child: const Text('Do something'), |
)<code_end> |
Flutter gives you access to a variety of buttons with predefined styles. |
The CupertinoButton class comes from the Cupertino library. |
Widgets in the Cupertino library use Apple’s design system.<topic_end> |
<topic_start> |
Aligning components horizontally |
In SwiftUI, stack views play a big part in designing your layouts. |
Two separate structures allow you to create stacks:HStack for horizontal stack viewsVStack for vertical stack viewsThe following SwiftUI view adds a globe image and |
text to a horizontal stack view:Flutter uses Row rather than HStack: |
<code_start> Row( |
mainAxisAlignment: MainAxisAlignment.center, |
children: [ |
Icon(CupertinoIcons.globe), |
Text('Hello, world!'), |
], |
),<code_end> |
The Row widget requires a List<Widget> in the children parameter. |
The mainAxisAlignment property tells Flutter how to position children |
with extra space. MainAxisAlignment.center positions children in the |
center of the main axis. For Row, the main axis is the horizontal |
axis.<topic_end> |
<topic_start> |
Aligning components vertically |
The following examples build on those in the previous section.In SwiftUI, you use VStack to arrange the components into a |
vertical pillar.Flutter uses the same Dart code from the previous example, |
except it swaps Column for Row: |
<code_start> Column( |
mainAxisAlignment: MainAxisAlignment.center, |
children: [ |
Icon(CupertinoIcons.globe), |
Text('Hello, world!'), |
], |
),<code_end> |
<topic_end> |
<topic_start> |
Displaying a list view |
In SwiftUI, you use the List base component to display sequences |
of items. |
To display a sequence of model objects, make sure that the user can |
identify your model objects. |
To make an object identifiable, use the Identifiable protocol.This resembles how Flutter prefers to build its list widgets. |
Flutter doesn’t need the list items to be identifiable. |
You set the number of items to display then build a widget for each item. |
<code_start>class Person { |
String name; |
Person(this.name); |
} |
var items = [ |
Person('Person 1'), |
Person('Person 2'), |
Person('Person 3'), |
]; |
class HomePage extends StatelessWidget { |
const HomePage({super.key}); |
@override |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.