Skip to content

Instantly share code, notes, and snippets.

@obouchari
Created May 6, 2020 13:37
Show Gist options
  • Save obouchari/67f6349e1668cca9aa1366d6a2e9efff to your computer and use it in GitHub Desktop.
Save obouchari/67f6349e1668cca9aa1366d6a2e9efff to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'first_page.dart';
import 'second_page.dart';
class BottomNavigationBarController extends StatefulWidget {
@override
_BottomNavigationBarControllerState createState() =>
_BottomNavigationBarControllerState();
}
class _BottomNavigationBarControllerState
extends State<BottomNavigationBarController> {
final List<Widget> pages = [
FirstPage(
key: PageStorageKey('Page1'),
),
SecondPage(
key: PageStorageKey('Page2'),
),
];
final PageStorageBucket bucket = PageStorageBucket();
int _selectedIndex = 0;
Widget _bottomNavigationBar(int selectedIndex) => BottomNavigationBar(
onTap: (int index) => setState(() => _selectedIndex = index),
currentIndex: selectedIndex,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.add), title: Text('First Page')),
BottomNavigationBarItem(
icon: Icon(Icons.list), title: Text('Second Page')),
],
);
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: _bottomNavigationBar(_selectedIndex),
body: PageStorage(
child: pages[_selectedIndex],
bucket: bucket,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment