Skip to content

Commit ae30a47

Browse files
committed
refactor(perf): Enhance state management and theme usage in form and navigation components
1 parent ac53dde commit ae30a47

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

lib/src/controls/flyouts/menu_bar.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:fluent_ui/fluent_ui.dart';
22
import 'package:flutter/foundation.dart';
3+
import 'package:flutter/scheduler.dart';
34
import 'package:flutter/services.dart';
45

56
/// Represents a top-level menu in a [MenuBar] control.
@@ -168,10 +169,11 @@ class MenuBarState extends State<MenuBar> {
168169
//
169170
// Even though the duration is zero, it is necessary to wait for the
170171
// transition to finish before showing the next flyout. Otherwise, the
171-
// flyout will fail to show due to [_locked]. This has a similar effect
172-
// to moving this task to the next frame or using a [Future.microtask].
173-
await Future<void>.delayed(Duration.zero);
174-
setState(() {});
172+
// flyout will fail to show due to [_locked]. Use SchedulerBinding for
173+
// frame-aligned updates instead of arbitrary delay.
174+
SchedulerBinding.instance.addPostFrameCallback((_) {
175+
if (mounted) setState(() {});
176+
});
175177
}
176178
}
177179

lib/src/controls/form/combo_box.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ class ComboBoxState<T> extends State<ComboBox<T>> {
12461246
}
12471247
}
12481248

1249-
TextStyle? get _textStyle =>
1249+
TextStyle? _textStyle(BuildContext context) =>
12501250
widget.style ?? FluentTheme.of(context).typography.body;
12511251

12521252
/// Opens the combo box popup.
@@ -1279,7 +1279,7 @@ class ComboBoxState<T> extends State<ComboBox<T>> {
12791279
from: context,
12801280
to: navigator.context,
12811281
),
1282-
style: _textStyle!,
1282+
style: _textStyle(context)!,
12831283
barrierLabel: FluentLocalizations.of(context).modalBarrierDismissLabel,
12841284
popupColor: widget.popupColor,
12851285
);
@@ -1327,6 +1327,7 @@ class ComboBoxState<T> extends State<ComboBox<T>> {
13271327
assert(debugCheckHasDirectionality(context));
13281328

13291329
final theme = FluentTheme.of(context);
1330+
final textStyle = _textStyle(context)!;
13301331

13311332
// The width of the button and the menu are defined by the widest
13321333
// item and the width of the placeholder.
@@ -1350,7 +1351,7 @@ class ComboBoxState<T> extends State<ComboBox<T>> {
13501351
placeholderIndex = items.length;
13511352
items.add(
13521353
DefaultTextStyle.merge(
1353-
style: _textStyle!.copyWith(
1354+
style: textStyle.copyWith(
13541355
color: theme.resources.textFillColorDisabled,
13551356
),
13561357
child: IgnorePointer(child: displayedHint),
@@ -1382,8 +1383,8 @@ class ComboBoxState<T> extends State<ComboBox<T>> {
13821383
builder: (context) {
13831384
return DefaultTextStyle.merge(
13841385
style: isEnabled
1385-
? _textStyle!
1386-
: _textStyle!.copyWith(
1386+
? textStyle
1387+
: textStyle.copyWith(
13871388
color: theme.resources.textFillColorDisabled,
13881389
),
13891390
child: Container(

lib/src/controls/form/number_box.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,6 @@ class NumberBoxState<T extends num> extends State<NumberBox<T>> {
565565

566566
if (_textBoxKey.currentContext != null) {
567567
Overlay.of(context).insert(_entry!);
568-
if (mounted) setState(() {});
569568
}
570569
}
571570

@@ -827,6 +826,7 @@ class _NumberBoxCompactOverlay extends StatelessWidget {
827826
@override
828827
Widget build(BuildContext context) {
829828
assert(debugCheckHasFluentTheme(context));
829+
final theme = FluentTheme.of(context);
830830

831831
return Padding(
832832
padding: const EdgeInsetsDirectional.only(start: 10),
@@ -840,10 +840,10 @@ class _NumberBoxCompactOverlay extends StatelessWidget {
840840
height: kNumberBoxOverlayHeight,
841841
width: kNumberBoxOverlayWidth,
842842
decoration: BoxDecoration(
843-
color: FluentTheme.of(context).menuColor,
843+
color: theme.menuColor,
844844
border: Border.all(
845845
width: 0.25,
846-
color: FluentTheme.of(context).inactiveBackgroundColor,
846+
color: theme.inactiveBackgroundColor,
847847
),
848848
),
849849
child: Column(

lib/src/controls/inputs/dropdown_button.dart

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ class DropDownButton extends StatefulWidget {
182182
return FadeTransition(opacity: animation, child: child);
183183
}
184184

185+
final animationCurve = FluentTheme.of(context).animationCurve;
185186
switch (placement) {
186187
case FlyoutPlacementMode.bottomCenter:
187188
case FlyoutPlacementMode.bottomLeft:
@@ -194,10 +195,7 @@ class DropDownButton extends StatefulWidget {
194195
begin: const Offset(0, -1),
195196
end: Offset.zero,
196197
).animate(
197-
CurvedAnimation(
198-
parent: animation,
199-
curve: FluentTheme.of(context).animationCurve,
200-
),
198+
CurvedAnimation(parent: animation, curve: animationCurve),
201199
),
202200
child: child,
203201
),
@@ -213,10 +211,7 @@ class DropDownButton extends StatefulWidget {
213211
begin: const Offset(0, 1),
214212
end: Offset.zero,
215213
).animate(
216-
CurvedAnimation(
217-
parent: animation,
218-
curve: FluentTheme.of(context).animationCurve,
219-
),
214+
CurvedAnimation(parent: animation, curve: animationCurve),
220215
),
221216
child: child,
222217
),

lib/src/controls/navigation/navigation_view/indicators.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ class NavigationIndicatorState<T extends NavigationIndicator> extends State<T> {
5858
);
5959
if (offsets != localOffsets) {
6060
offsets = localOffsets;
61+
if (mounted) setState(() {});
6162
}
62-
63-
if (mounted) setState(() {});
6463
});
6564
}
6665

0 commit comments

Comments
 (0)