Consider this Flutter widget with a TabBar and TabBarView. What will the user see when the app runs?
DefaultTabController( length: 2, child: Scaffold( appBar: AppBar( bottom: TabBar( tabs: [ Tab(icon: Icon(Icons.home)), Tab(icon: Icon(Icons.settings)), ], ), title: Text('Tabs Example'), ), body: TabBarView( children: [ Center(child: Text('Home Page')), Center(child: Text('Settings Page')), ], ), ), )
Check how many tabs and TabBarView children are defined and what widgets they contain.
The DefaultTabController manages the tab selection. The TabBar shows two tabs with icons. The TabBarView has two children widgets matching the tabs. Tapping a tab switches the displayed child widget accordingly.
Given this Flutter code with TabBarView and stateful children, what lifecycle behavior occurs when switching tabs?
DefaultTabController( length: 2, child: Scaffold( appBar: AppBar( bottom: TabBar(tabs: [Tab(text: 'A'), Tab(text: 'B')]), ), body: TabBarView( children: [ StatefulWidgetA(), StatefulWidgetB(), ], ), ), ) class StatefulWidgetA extends StatefulWidget { @override State<StatefulWidgetA> createState() => _StatefulWidgetAState(); } class _StatefulWidgetAState extends State<StatefulWidgetA> { @override void initState() { super.initState(); print('A initState'); } @override Widget build(BuildContext context) { return Center(child: Text('Widget A')); } }
Think about how TabBarView keeps its children alive by default.
TabBarView keeps all its children alive in memory by default, so each child's initState runs once when first created. Switching tabs only changes visibility, not widget recreation.
You want to switch to the second tab in a DefaultTabController from code (not user tap). Which code snippet does this correctly?
DefaultTabController( length: 3, child: Scaffold( appBar: AppBar( bottom: TabBar(tabs: [Tab(text: 'One'), Tab(text: 'Two'), Tab(text: 'Three')]), ), body: TabBarView(children: [Text('1'), Text('2'), Text('3')]), ), )
Look for the official method to animate tab changes on DefaultTabController.
DefaultTabController.of(context) returns the TabController. The animateTo(index) method changes the selected tab with animation. Other options use incorrect method names or classes.
Analyze this Flutter code snippet. What error will it cause?
DefaultTabController( length: 2, child: Scaffold( appBar: AppBar( bottom: TabBar( tabs: [ Tab(text: 'Tab 1'), ], ), title: Text('Example'), ), body: TabBarView( children: [ Center(child: Text('Page 1')), Center(child: Text('Page 2')), ], ), ), )
Check the number of tabs vs the number of TabBarView children.
TabBar and TabBarView must have the same number of tabs and children. Mismatch causes an assertion error at runtime.
Given this Flutter code, tapping tabs does not change the displayed page. What is the cause?
class MyTabs extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( bottom: TabBar( tabs: [Tab(text: 'A'), Tab(text: 'B')], ), title: Text('Tabs'), ), body: TabBarView( children: [ Center(child: Text('Page A')), Center(child: Text('Page B')), ], ), ); } }
Check if a controller is managing the tabs.
TabBar and TabBarView need a TabController to coordinate tab selection and page display. Without DefaultTabController or a custom controller, tapping tabs does nothing.