Нижняя панель NavigationBar в Flutter
Несколько равноправных разделов удобно
переключать панелью внизу. В Material 3
для этого служит
NavigationBar
с пунктами
NavigationDestination - не путайте
с устаревшим BottomNavigationBar.
Индекс выбранного пункта хранят в
State. Параметр selectedIndex
и onDestinationSelected обновляют
его через setState.
Содержимое экрана - отдельные виджеты,
которые подставляют в body по
индексу:
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: ShellPage()));
}
class ShellPage extends StatefulWidget {
const ShellPage({super.key});
@override
State<ShellPage> createState() => _ShellPageState();
}
class _ShellPageState extends State<ShellPage> {
int index = 0;
@override
Widget build(BuildContext context) {
final pages = [
const Center(child: Text('Feed')),
const Center(child: Text('Search')),
const Center(child: Text('Profile')),
];
return Scaffold(
body: pages[index],
bottomNavigationBar: NavigationBar(
selectedIndex: index,
onDestinationSelected: (value) {
setState(() {
index = value;
});
},
destinations: const [
NavigationDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: 'Feed',
),
NavigationDestination(
icon: Icon(Icons.search),
label: 'Search',
),
NavigationDestination(
icon: Icon(Icons.person_outline),
selectedIcon: Icon(Icons.person),
label: 'Profile',
),
],
),
);
}
}
Сделайте три вкладки: «Home», «Favorites»,
«More». На «Favorites» покажите
ListView
из трёх строк. Индекс храните в
State, как в примере.