Skip to content

Commit 26c707d

Browse files
committed
correctly de-register font listener
1 parent 02d5737 commit 26c707d

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

lib/src/controls/font_family.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ class FontFamilyDropdown extends StatefulWidget {
1717

1818
class _FontFamilyDropdownState extends State<FontFamilyDropdown> {
1919
UnicodeFont? _currentFont;
20+
late TextEditorApi _api;
21+
22+
@override
23+
void didChangeDependencies() {
24+
final api = TextEditorApiWidget.of(context)!.editorApi;
25+
_api = api;
26+
api.addFontListener(_onFontChanged);
27+
super.didChangeDependencies();
28+
}
2029

2130
@override
2231
void dispose() {
23-
final api = TextEditorApiWidget.of(context)?.editorApi;
24-
api?.removeFontListener(_onFontChanged);
32+
_api.removeFontListener(_onFontChanged);
2533
super.dispose();
2634
}
2735

@@ -35,8 +43,6 @@ class _FontFamilyDropdownState extends State<FontFamilyDropdown> {
3543

3644
@override
3745
Widget build(BuildContext context) {
38-
final api = TextEditorApiWidget.of(context)!.editorApi;
39-
api.addFontListener(_onFontChanged);
4046
const selectedTextStyle = TextStyle(fontSize: 12);
4147
return PlatformDropdownButton<UnicodeFont>(
4248
value: _currentFont,
@@ -45,7 +51,7 @@ class _FontFamilyDropdownState extends State<FontFamilyDropdown> {
4551
setState(() {
4652
_currentFont = value;
4753
});
48-
api.setFont(value);
54+
_api.setFont(value);
4955
}
5056
},
5157
selectedItemBuilder: (context) => UnicodeFont.values

lib/src/controls/standard_formatting.dart

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ class BaseFormatButtons extends StatefulWidget {
1818
class _BaseFormatButtonsState extends State<BaseFormatButtons> {
1919
final _isSelected = [false, false, false, false];
2020

21+
late TextEditorApi _api;
22+
23+
@override
24+
void didChangeDependencies() {
25+
final api = TextEditorApiWidget.of(context)!.editorApi;
26+
_api = api;
27+
api.addFontListener(_onFontChanged);
28+
super.didChangeDependencies();
29+
}
30+
31+
@override
32+
void dispose() {
33+
_api.removeFontListener(_onFontChanged);
34+
super.dispose();
35+
}
36+
2137
void _onFontChanged(UnicodeFont font) {
2238
if (mounted) {
2339
setState(() {
@@ -29,18 +45,9 @@ class _BaseFormatButtonsState extends State<BaseFormatButtons> {
2945
}
3046
}
3147

32-
@override
33-
void dispose() {
34-
final api = TextEditorApiWidget.of(context)?.editorApi;
35-
api?.removeFontListener(_onFontChanged);
36-
super.dispose();
37-
}
38-
3948
@override
4049
Widget build(BuildContext context) {
41-
final api = TextEditorApiWidget.of(context)!.editorApi;
42-
api.addFontListener(_onFontChanged);
43-
50+
final api = _api;
4451
return PlatformToggleButtons(
4552
children: [
4653
Icon(CommonPlatformIcons.bold),

0 commit comments

Comments
 (0)