Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FocusScope.of(context).requestFocus(new MyFocusNode()) memleak test
import 'package:flutter/material.dart';
class MyFocusNode extends FocusNode {
MyFocusNode() : super() {
print("constructor");
}
@override
void dispose() {
print("dispose");
super.dispose();
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark(),
debugShowCheckedModeBanner: false,
home: Screen1(),
);
}
}
class Screen1 extends StatefulWidget {
@override
_Screen1State createState() => _Screen1State();
}
class _Screen1State extends State<Screen1> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("unfocus"),
onPressed: () =>
FocusScope.of(context).requestFocus(new MyFocusNode()),
),
RaisedButton(
child: Text("next page"),
onPressed: () => Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (ctx) => Screen2()),
),
)
],
),
),
);
}
@override
void dispose() {
print("dispose screen1");
super.dispose();
}
}
class Screen2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text("go back"),
onPressed: () => Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (ctx) => Screen1()),
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment