import 'package:flutter/material.dart';
class TabNavigationScreen extends StatefulWidget {
@override
State<TabNavigationScreen> createState() => _TabNavigationScreenState();
}
class _TabNavigationScreenState extends State<TabNavigationScreen> {
int _selectedIndex = 0;
static const List<Widget> _pages = <Widget>[
Center(child: Text('Home Screen', style: TextStyle(fontSize: 24))),
Center(child: Text('Search Screen', style: TextStyle(fontSize: 24))),
Center(child: Text('Profile Screen', style: TextStyle(fontSize: 24))),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Simple Tab Navigation'),
),
body: _pages[_selectedIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedIndex,
onTap: _onItemTapped,
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Search',
),
BottomNavigationBarItem(
icon: Icon(Icons.person),
label: 'Profile',
),
],
),
);
}
}
This solution uses a StatefulWidget to keep track of the selected tab index with _selectedIndex. We define a static list _pages containing three Center widgets each showing the text for Home, Search, and Profile screens.
The BottomNavigationBar has three items with icons and labels. When a tab is tapped, _onItemTapped updates the selected index and rebuilds the UI.
The body of the Scaffold shows the widget from _pages matching the selected index, so the content changes accordingly.
This creates a simple tab navigation experience familiar in many apps.