I have a timer on my main page and when i navigate to named route the timer keep working and want it to dispose so i used pushReplacement to new screen and back, now my problem is i lose my side bar which i initialize in main, i can move it to an layout widget and rebuild it also but it is something that should happen one time so It seems to me a wrong way
Is pushReplacement realy required?
There is an other way to achieve this behavior?
this is my main:
@override
Widget build(BuildContext context) {
print('build');
return MultiProvider(
providers: _providers,
child: MaterialApp(
title: 'myProject',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: AppColors.primaryBlack,
brightness: Brightness.light,
),
home: FutureBuilder(
future: _initFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Scaffold(
body: Stack(children: [
AppLayout(
currentIndex: 0,
),
SideBar(),
]),
);
} else {
return SplashScreen();
}
},
),
),
);
}
my route page that contain the pages i push and replace:
class RoutePage extends StatelessWidget {
final String back;
final Widget child;
final Color color;
final dynamic args;
const RoutePage({Key key, this.child, this.color, this.args, this.back})
: super(key: key);
void navBack(context) {
switch (back) {
case "home":
AppRoutes.home(context, 0);
break;
case "leagues":
AppRoutes.home(context, 1);
break;
case "liveScores":
AppRoutes.home(context, 2);
break;
default:
}
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
if (this.back != null) {
navBack(context);
} else {
Navigator.pop(context);
}
return Future.value(true);
},
child: Scaffold(
body: Stack(
children: [
child,
Positioned(
top: 30,
left: 30,
child: IconButton(
color: color != null ? color : Colors.black,
icon: Icon(Icons.arrow_back),
onPressed: () {
if (this.back != null) {
navBack(context);
} else {
Navigator.pop(context);
}
},
)),
],
),
),
);
}
}
approutes.home:
static home(context, index) => Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext context) => AppLayout(currentIndex: index)));
question from:
https://stackoverflow.com/questions/65925884/navigation-to-new-route-and-dispose-the-state-before 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…