Created
October 23, 2019 11:00
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When clicking the TextField and then hitting Back a couple of times the Bloc is created every time: