Skip to content

Commit 618193a

Browse files
authored
Update case study on memory leaks. (flutter#4942)
1 parent adb1097 commit 618193a

File tree

1 file changed

+6
-7
lines changed
  • case_study/memory_leaks/leaking_counter_1/lib

1 file changed

+6
-7
lines changed

case_study/memory_leaks/leaking_counter_1/lib/main.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class MyHomePage extends StatefulWidget {
2929
}
3030

3131
class MyIncrementer {
32-
MyIncrementer(this.increment, this.screen);
33-
final Scaffold? screen;
32+
MyIncrementer(this.increment);
3433
final VoidCallback increment;
3534
}
3635

@@ -41,27 +40,26 @@ class _MyHomePageState extends State<MyHomePage> {
4140
() => setState(() {
4241
_counter++;
4342
}),
44-
null,
4543
);
4644

4745
/// Increments counter if current screen contains floating action button.
4846
void _incrementCounter(BuildContext context) {
4947
final oldIncrementer = _incrementer;
48+
final screen = buildScreen(context);
5049

5150
_incrementer = MyIncrementer(
5251
() {
53-
final screen = theScreen;
5452
if (screen.floatingActionButton != null) {
5553
oldIncrementer.increment();
5654
}
5755
},
58-
theScreen,
5956
);
6057

6158
_incrementer.increment();
6259
}
6360

64-
Scaffold get theScreen {
61+
Scaffold buildScreen(BuildContext context) {
62+
final theme = Theme.of(context);
6563
return Scaffold(
6664
appBar: AppBar(
6765
title: Text(widget.title),
@@ -80,13 +78,14 @@ class _MyHomePageState extends State<MyHomePage> {
8078
floatingActionButton: FloatingActionButton(
8179
onPressed: () => _incrementCounter(context),
8280
tooltip: 'Increment counter',
81+
foregroundColor: theme.canvasColor,
8382
child: const Icon(Icons.add),
8483
),
8584
);
8685
}
8786

8887
@override
89-
Widget build(BuildContext context) => theScreen;
88+
Widget build(BuildContext context) => buildScreen(context);
9089
}
9190

9291
class MyCounter extends StatelessWidget {

0 commit comments

Comments
 (0)