Skip to content

Instantly share code, notes, and snippets.

@Sunbreak
Created March 10, 2022 11:45
Show Gist options
  • Save Sunbreak/d128306cb25e0476704358afa380a961 to your computer and use it in GitHub Desktop.
Save Sunbreak/d128306cb25e0476704358afa380a961 to your computer and use it in GitHub Desktop.
Flutter-BottomNavigationBar-KeepAlive
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final indexes = List.generate(10, (index) => index);
PageController _pageController = PageController();
@override
void initState() {
super.initState();
_pageController = PageController();
}
@override
void dispose() {
super.dispose();
_pageController.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
bottomNavigationBar: StatefulNaviBar(
items: [
for (var i in indexes)
BottomNavigationBarItem(
icon: const Icon(Icons.abc),
label: 'l$i',
backgroundColor: Colors.grey,
),
],
onTap: (index) => _pageController.jumpToPage(index),
),
body: PageView(
controller: _pageController,
physics: const NeverScrollableScrollPhysics(),
children: [
...indexes.map((i) {
return i % 2 == 0 ? PlainWidget(i: i) : AliveWidget(i: i);
})
],
),
);
}
}
class StatefulNaviBar extends StatefulWidget {
const StatefulNaviBar({
Key? key,
required this.items,
this.onTap,
this.initialIndex = 0,
}) : super(key: key);
final List<BottomNavigationBarItem> items;
final ValueChanged<int>? onTap;
final int initialIndex;
@override
State<StatefulWidget> createState() => _StatefulNaviBarState();
}
class _StatefulNaviBarState extends State<StatefulNaviBar> {
int _selectedIndex = 0;
@override
void initState() {
super.initState();
_selectedIndex = widget.initialIndex;
}
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
currentIndex: _selectedIndex,
items: widget.items,
onTap: (index) {
setState(() => _selectedIndex = index);
widget.onTap?.call(index);
},
);
}
}
class PlainWidget extends StatelessWidget {
const PlainWidget({
Key? key,
required this.i,
}) : super(key: key);
final int i;
@override
Widget build(BuildContext context) {
debugPrint('PlainWidget build $i');
return Center(
child: Text('PlainWidget$i'),
);
}
}
class AliveWidget extends StatefulWidget {
const AliveWidget({
Key? key,
required this.i,
}) : super(key: key);
final int i;
@override
State<StatefulWidget> createState() => AliveWidgetState();
}
class AliveWidgetState extends State<AliveWidget> with AutomaticKeepAliveClientMixin {
@override
bool get wantKeepAlive => true;
@override
Widget build(BuildContext context) {
super.build(context);
debugPrint('AliveWidget build ${widget.i}');
return Center(
child: Text('AliveWidget${widget.i}'),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment