07-21-2023, 04:41 PM
# Summary of Solutions: Two Approaches Offered #
## Solution 1: AutomaticKeepAliveClientMixin ##
class SubPage extends StatefulWidget {
@override
State<SubPage> createState() => _SubPageState();
}
class _SubPageState extends State<SubPage> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context); // Ensure that the mixin is initialized
return Container();
}
@override
bool get wantKeepAlive => true;
}
/*-------------------------------*/
class MainPage extends StatefulWidget {
const MainPage({Key? key}) : super(key: key);
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int currentBottom = 0;
static const List<Widget> _page = [
SubPage(),
SubPage(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentBottom,
onTap: (index) => setState(() {
currentBottom = index;
}),
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home)),
BottomNavigationBarItem(icon: Icon(Icons.settings))
],
),
body: _page.elementAt(currentBottom));
}
}
## Solution 2: IndexedStack ##
class MainPage extends StatefulWidget {
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: <Widget>[
Page1(),
Page2(),
Page3(),
],
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (int index) {
setState(() {
_currentIndex = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Page 1',
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Page 2',
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Page 3',
),
],
),
);
}
}
/*--------------------------------*/
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
// Page 1 content
);
}
}
// Repeat the above for Page2 and Page3
## Solution 1: AutomaticKeepAliveClientMixin ##
class SubPage extends StatefulWidget {
@override
State<SubPage> createState() => _SubPageState();
}
class _SubPageState extends State<SubPage> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context); // Ensure that the mixin is initialized
return Container();
}
@override
bool get wantKeepAlive => true;
}
/*-------------------------------*/
class MainPage extends StatefulWidget {
const MainPage({Key? key}) : super(key: key);
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int currentBottom = 0;
static const List<Widget> _page = [
SubPage(),
SubPage(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
currentIndex: currentBottom,
onTap: (index) => setState(() {
currentBottom = index;
}),
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home)),
BottomNavigationBarItem(icon: Icon(Icons.settings))
],
),
body: _page.elementAt(currentBottom));
}
}
## Solution 2: IndexedStack ##
class MainPage extends StatefulWidget {
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: _currentIndex,
children: <Widget>[
Page1(),
Page2(),
Page3(),
],
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (int index) {
setState(() {
_currentIndex = index;
});
},
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Page 1',
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Page 2',
),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Page 3',
),
],
),
);
}
}
/*--------------------------------*/
class Page1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
// Page 1 content
);
}
}
// Repeat the above for Page2 and Page3