text stringlengths 1 474 |
|---|
);<code_end> |
<topic_end> |
<topic_start> |
2. Create the tabs |
When a tab is selected, it needs to display content. |
You can create tabs using the TabBar widget. |
In this example, create a TabBar with three |
Tab widgets and place it within an AppBar. |
<code_start>return MaterialApp( |
home: DefaultTabController( |
length: 3, |
child: Scaffold( |
appBar: AppBar( |
bottom: const TabBar( |
tabs: [ |
Tab(icon: Icon(Icons.directions_car)), |
Tab(icon: Icon(Icons.directions_transit)), |
Tab(icon: Icon(Icons.directions_bike)), |
], |
), |
), |
), |
), |
);<code_end> |
By default, the TabBar looks up the widget tree for the nearest |
DefaultTabController. If you’re manually creating a TabController, |
pass it to the TabBar.<topic_end> |
<topic_start> |
3. Create content for each tab |
Now that you have tabs, display content when a tab is selected. |
For this purpose, use the TabBarView widget.info Note |
Order is important and must correspond to the order of the tabs in the |
TabBar. |
<code_start>body: const TabBarView( |
children: [ |
Icon(Icons.directions_car), |
Icon(Icons.directions_transit), |
Icon(Icons.directions_bike), |
], |
),<code_end> |
<topic_end> |
<topic_start> |
Interactive example |
<code_start>import 'package:flutter/material.dart'; |
void main() { |
runApp(const TabBarDemo()); |
} |
class TabBarDemo extends StatelessWidget { |
const TabBarDemo({super.key}); |
@override |
Widget build(BuildContext context) { |
return MaterialApp( |
home: DefaultTabController( |
length: 3, |
child: Scaffold( |
appBar: AppBar( |
bottom: const TabBar( |
tabs: [ |
Tab(icon: Icon(Icons.directions_car)), |
Tab(icon: Icon(Icons.directions_transit)), |
Tab(icon: Icon(Icons.directions_bike)), |
], |
), |
title: const Text('Tabs Demo'), |
), |
body: const TabBarView( |
children: [ |
Icon(Icons.directions_car), |
Icon(Icons.directions_transit), |
Icon(Icons.directions_bike), |
], |
), |
), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start>Navigate to a new screen and back |
Most apps contain several screens for displaying different types of |
information. |
For example, an app might have a screen that displays products. |
When the user taps the image of a product, a new screen displays |
details about the product.Terminology: In Flutter, screens and pages are called routes. |
The remainder of this recipe refers to routes.In Android, a route is equivalent to an Activity. |
In iOS, a route is equivalent to a ViewController. |
In Flutter, a route is just a widget.This recipe uses the Navigator to navigate to a new route.The next few sections show how to navigate between two routes, |
using these steps:<topic_end> |
<topic_start> |
1. Create two routes |
First, create two routes to work with. Since this is a basic example, |
each route contains only a single button. Tapping the button on the |
first route navigates to the second route. Tapping the button on the |
second route returns to the first route.First, set up the visual structure: |
<code_start>class FirstRoute extends StatelessWidget { |
const FirstRoute({super.key}); |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar( |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.