I have widget that is unmounted on page change.
I am using flutter web.
I looked at all the methods here but I can't seem to find the one.
I tried
@override
void dispose() {
print("dispose?");
super.dispose();
}
@override
void deactivate() {
print("deactivate");
super.deactivate();
}
and neither is called.
It is a very simple thing I need to detect when the widget is not being "built" (rendered). I don't think having something to dtect the page change is the solution, it seems overkill. Thank you
Adding code sample
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: 'Named Routes Demo',
initialRoute: '/',
routes: {
'/': (context) => FirstScreen(),
'/second': (context) => SecondScreen(),
},
));
}
class FirstScreen extends StatefulWidget {
FirstScreen({Key key}) : super(key: key);
@override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
@override
void dispose() {
print("dispose");
super.dispose();
}
@override
void deactivate() {
print("deactivate");
super.deactivate();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('First Screen'),
),
body: Center(
child: ElevatedButton(
child: Text('Launch screen'),
onPressed: () {
Navigator.pushNamed(context, '/second');
},
),
),
);
}
}
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Second Screen"),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// Navigate back to the first screen by popping the current route
// off the stack.
Navigator.pop(context);
},
child: Text('Go back!'),
),
),
);
}
}
It seems neither deactive nor dispose are called when navigating to the second screen and the first screen widget stops being part of the render tree. So what to call to detect that Screen 1 leaves the render tree?