text
stringlengths
1
372
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>