Skip to content

Instantly share code, notes, and snippets.

@amitkot
Created October 23, 2019 11:00
Show Gist options
  • Save amitkot/b3c94fa172e2a6541707fe837646fd0d to your computer and use it in GitHub Desktop.
Save amitkot/b3c94fa172e2a6541707fe837646fd0d to your computer and use it in GitHub Desktop.
An example of a page that does expensive work when recreated every time the keyboard pops on and off
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: MyHomePage());
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Test Expensive Work'),
),
body: Center(
child: RaisedButton(
child: Text('Open Page'),
onPressed: () => _openExpensivePage(context),
),
),
);
}
_openExpensivePage(BuildContext context) {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) =>
ExpensivePage(bloc: ExpensivePageBloc())));
}
}
class ExpensivePage extends StatefulWidget {
ExpensivePage({@required this.bloc}) : assert(bloc != null);
final ExpensivePageBloc bloc;
@override
_ExpensivePageState createState() => _ExpensivePageState();
}
class _ExpensivePageState extends State<ExpensivePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(32.0),
child: Center(
child: TextField(
decoration: InputDecoration(hintText: 'Enter some text'),
),
),
),
);
}
}
class ExpensivePageBloc {
ExpensivePageBloc() {
print('!!! doing something expensive');
}
}
@amitkot
Copy link
Author

amitkot commented Oct 23, 2019

When clicking the TextField and then hitting Back a couple of times the Bloc is created every time:

13:57:32.370 4 info flutter.tools I/flutter (17940): !!! doing something expensive
13:57:34.124 5 info flutter.tools I/flutter (17940): !!! doing something expensive
13:57:35.629 6 info flutter.tools I/flutter (17940): !!! doing something expensive
13:57:36.997 7 info flutter.tools I/flutter (17940): !!! doing something expensive
13:57:38.834 8 info flutter.tools I/flutter (17940): !!! doing something expensive
13:57:40.135 9 info flutter.tools I/flutter (17940): !!! doing something expensive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment