I'm working with IndexedStack in order not to rebuild each page while using BottomNavigationBar
// MAIN.DART
class LoggedHandle extends StatefulWidget {
const LoggedHandle({Key? key}) : super(key: key);
@override
State<LoggedHandle> createState() => _LoggedHandleState();
}
class _LoggedHandleState extends State<LoggedHandle> {
double height = AppBar().preferredSize.height;
int _selectedPage = 1;
@override
void initState() {
});
super.initState();
}
@override
void dispose() {
_importo.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedPage,
onTap: (int index) {
setState(() {
_selectedPage = index;
});
},
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.receipt),
label: 'Schedina',
),
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
label: 'Account',
),
]),
body: IndexedStack(
index: _selectedPage,
children: [BetView(), HomeView(), UserView()],
),
);
}
}
// HOME.DART
class HomeView extends StatefulWidget {
const HomeView({Key? key}) : super(key: key);
@override
State<HomeView> createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return SizedBox.expand(
child: SingleChildScrollView(
child: Column(
children: [
Padding(
child: Text("rebuild this")
),
],
),
),
);
}
}
In BetView()
I have a ElevatedButton
and I would like to rebuild HomeView
when it is pressed. Navigation to HomeView()
won't rebuild it because of IndexedStack
// BETVIEW
class BetView extends StatefulWidget {
const BetView({Key? key}) : super(key: key);
@override
State<BetView> createState() => _BetViewState();
}
class _BetViewState extends State<BetView> {
late final TextEditingController _importo;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return BlocBuilder<BetBloc, BetState>(
builder: (context, state) {
if (state is BetLoaded) {
double quotatotale = 1.0;
for (BetEntry bet in state.bets) {
print(bet);
quotatotale = quotatotale * bet.quota;
}
return Column(
children: [
Padding(
padding: const EdgeInsets.all(10.0),
child: Row(
children: [
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.orange[500]),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
)
)
),
onPressed: (){
// Rebuild HomeView()
},
child: const Padding(
padding: EdgeInsets.all(15),
child: Text("Elimina tutte",
style: TextStyle(
fontSize: 15,
color: Colors.white,
),
),
),
),
const Spacer(),
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.green),
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
)
)
),
onPressed: (){
},
child: const Padding(
padding: EdgeInsets.all(15),
child: Text("Conferma",
style: TextStyle(
fontSize: 15
),
),
),
),
],
),
),
],
);
} else {
return const Text("something wrong");
}
},
);
}
}