Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 271 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to preserve widget states in flutter, when navigating using BottomNavigationBar?

#11
# 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

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through