Skip to content

Commit

Permalink
MDS-501 RepaintBoundary and mounted usage
Browse files Browse the repository at this point in the history
  • Loading branch information
GittHub-d committed Apr 18, 2023
1 parent 0c7c38d commit 906386c
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 260 deletions.
28 changes: 14 additions & 14 deletions lib/src/theme/authcode/authcode_properties.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ class MoonAuthCodeProperties extends ThemeExtension<MoonAuthCodeProperties> with
/// AuthCode error text style.
final TextStyle errorTextStyle;

const MoonAuthCodeProperties({
required this.borderRadius,
required this.gap,
required this.height,
required this.width,
required this.animationDuration,
required this.errorAnimationDuration,
required this.peekDuration,
required this.animationCurve,
required this.errorAnimationCurve,
required this.textStyle,
required this.errorTextStyle,
});

@override
MoonAuthCodeProperties copyWith({
BorderRadius? borderRadius,
Expand Down Expand Up @@ -104,20 +118,6 @@ class MoonAuthCodeProperties extends ThemeExtension<MoonAuthCodeProperties> with
);
}

const MoonAuthCodeProperties({
required this.borderRadius,
required this.gap,
required this.height,
required this.width,
required this.animationDuration,
required this.errorAnimationDuration,
required this.peekDuration,
required this.animationCurve,
required this.errorAnimationCurve,
required this.textStyle,
required this.errorTextStyle,
});

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
Expand Down
57 changes: 28 additions & 29 deletions lib/src/widgets/accordion/accordion_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,24 +229,23 @@ class _MoonAccordionItemState<T> extends State<MoonAccordionItem<T>> with Single
bool _isHovered = false;

FocusNode? _focusNode;

FocusNode get _effectiveFocusNode => widget.focusNode ?? (_focusNode ??= FocusNode());

void _handleHover(bool hover) {
if (hover != _isHovered && mounted) {
if (hover != _isHovered) {
setState(() => _isHovered = hover);
}
}

void _handleFocus(bool focus) {
if (focus != _isFocused && mounted) {
if (focus != _isFocused) {
setState(() => _isFocused = focus);
}
}

void _handleFocusChange(bool hasFocus) {
setState(() {
_isFocused = hasFocus;
});
setState(() => _isFocused = hasFocus);
}

void _handleTap() {
Expand All @@ -256,9 +255,8 @@ class _MoonAccordionItemState<T> extends State<MoonAccordionItem<T>> with Single
_animationController!.forward();
} else {
_animationController!.reverse().then<void>((void value) {
if (!mounted) {
return;
}
if (!mounted) return;

setState(() {
// Rebuild without widget.children.
});
Expand Down Expand Up @@ -316,6 +314,7 @@ class _MoonAccordionItemState<T> extends State<MoonAccordionItem<T>> with Single
@override
void didUpdateWidget(MoonAccordionItem<T> oldWidget) {
super.didUpdateWidget(oldWidget);

if (widget.identityValue == null && widget.groupIdentityValue == null) return;

if (widget._selected) {
Expand All @@ -329,9 +328,8 @@ class _MoonAccordionItemState<T> extends State<MoonAccordionItem<T>> with Single
_animationController!.forward();
} else {
_animationController!.reverse().then<void>((void value) {
if (!mounted) {
return;
}
if (!mounted) return;

setState(() {
// Rebuild without widget.children.
});
Expand All @@ -344,6 +342,7 @@ class _MoonAccordionItemState<T> extends State<MoonAccordionItem<T>> with Single
@override
void dispose() {
_animationController!.dispose();

super.dispose();
}

Expand Down Expand Up @@ -455,25 +454,25 @@ class _MoonAccordionItemState<T> extends State<MoonAccordionItem<T>> with Single
onFocusChange: _handleFocusChange,
onShowFocusHighlight: _handleFocus,
onShowHoverHighlight: _handleHover,
child: RepaintBoundary(
child: MoonFocusEffect(
show: _isFocused,
effectExtent: effectiveFocusEffectExtent,
effectColor: effectiveFocusEffectColor,
effectDuration: effectiveFocusEffectDuration,
effectCurve: effectiveFocusEffectCurve,
childBorderRadius: effectiveBorderRadius,
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: _handleTap,
child: AnimatedIconTheme(
child: MoonFocusEffect(
show: _isFocused,
effectExtent: effectiveFocusEffectExtent,
effectColor: effectiveFocusEffectColor,
effectDuration: effectiveFocusEffectDuration,
effectCurve: effectiveFocusEffectCurve,
childBorderRadius: effectiveBorderRadius,
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: _handleTap,
child: AnimatedIconTheme(
duration: effectiveTransitionDuration,
color: effectiveTextColor,
child: AnimatedDefaultTextStyle(
style: DefaultTextStyle.of(context).style.copyWith(color: effectiveTextColor),
duration: effectiveTransitionDuration,
color: effectiveTextColor,
child: AnimatedDefaultTextStyle(
style: DefaultTextStyle.of(context).style.copyWith(color: effectiveTextColor),
duration: effectiveTransitionDuration,
child: RepaintBoundary(
child: AnimatedContainer(
duration: effectiveHoverEffectDuration,
curve: effectiveHoverEffectCurve,
Expand Down
17 changes: 7 additions & 10 deletions lib/src/widgets/alert/alert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,12 @@ class _MoonAlertState extends State<MoonAlert> with SingleTickerProviderStateMix
}

void _showAlert() {
if (!mounted) return;
_animationController!.forward();

setState(() => _isVisible = true);
}

void _hideAlert() {
if (!mounted) return;

_animationController!.reverse().then<void>((void value) {
if (!mounted) return;

Expand All @@ -159,6 +156,7 @@ class _MoonAlertState extends State<MoonAlert> with SingleTickerProviderStateMix
@override
void initState() {
super.initState();

WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;

Expand All @@ -171,13 +169,12 @@ class _MoonAlertState extends State<MoonAlert> with SingleTickerProviderStateMix
@override
void didUpdateWidget(MoonAlert oldWidget) {
super.didUpdateWidget(oldWidget);
if (mounted) {
if (oldWidget.show != widget.show) {
if (widget.show) {
_showAlert();
} else {
_hideAlert();
}

if (oldWidget.show != widget.show) {
if (widget.show) {
_showAlert();
} else {
_hideAlert();
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions lib/src/widgets/authcode/authcode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ enum AuthFieldShape { box, underline, circle }

enum ErrorAnimationType { noAnimation, shake }

typedef MoonAuthCodeBuilder = Widget Function(
BuildContext context,
String? errorText,
);
typedef MoonAuthCodeErrorBuilder = Widget Function(BuildContext context, String? errorText);

class MoonAuthCode extends StatefulWidget {
/// Shape of auth input field.
Expand Down Expand Up @@ -144,7 +141,7 @@ class MoonAuthCode extends StatefulWidget {
final MainAxisAlignment mainAxisAlignment;

/// Builder for the error widget.
final MoonAuthCodeBuilder errorBuilder;
final MoonAuthCodeErrorBuilder errorBuilder;

/// Displays a hint or a placeholder in the input field if it's value is empty.
final String? hintCharacter;
Expand Down
53 changes: 30 additions & 23 deletions lib/src/widgets/checkbox/checkbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ class MoonCheckbox extends StatefulWidget {
autofocus: autofocus,
),
const SizedBox(width: 12),
AnimatedOpacity(
opacity: isInteractive ? 1 : effectiveDisabledOpacityValue,
duration: effectiveFocusEffectDuration,
child: Text(
label,
style: effectiveTextStyle,
RepaintBoundary(
child: AnimatedOpacity(
opacity: isInteractive ? 1 : effectiveDisabledOpacityValue,
duration: effectiveFocusEffectDuration,
child: Text(
label,
style: effectiveTextStyle,
),
),
),
],
Expand Down Expand Up @@ -171,12 +173,14 @@ class _MoonCheckboxState extends State<MoonCheckbox> with TickerProviderStateMix
@override
void initState() {
super.initState();

_previousValue = widget.value;
}

@override
void didUpdateWidget(MoonCheckbox oldWidget) {
super.didUpdateWidget(oldWidget);

if (oldWidget.value != widget.value) {
_previousValue = oldWidget.value;
animateToValue();
Expand All @@ -186,6 +190,7 @@ class _MoonCheckboxState extends State<MoonCheckbox> with TickerProviderStateMix
@override
void dispose() {
_painter.dispose();

super.dispose();
}

Expand Down Expand Up @@ -240,23 +245,25 @@ class _MoonCheckboxState extends State<MoonCheckbox> with TickerProviderStateMix
effectColor: effectiveFocusEffectColor,
effectCurve: effectiveFocusEffectCurve,
effectDuration: effectiveFocusEffectDuration,
child: AnimatedOpacity(
opacity: states.contains(MaterialState.disabled) ? effectiveDisabledOpacityValue : 1,
duration: effectiveFocusEffectDuration,
child: buildToggleable(
mouseCursor: effectiveMouseCursor,
focusNode: widget.focusNode,
autofocus: widget.autofocus,
size: size,
painter: _painter
..position = position
..activeColor = effectiveActiveColor
..inactiveColor = effectiveInactiveColor
..checkColor = effectiveCheckColor
..value = value
..previousValue = _previousValue
..shape = SmoothRectangleBorder(borderRadius: effectiveBorderRadius.smoothBorderRadius)
..side = _resolveSide(BorderSide(color: effectiveBorderColor)),
child: RepaintBoundary(
child: AnimatedOpacity(
opacity: states.contains(MaterialState.disabled) ? effectiveDisabledOpacityValue : 1,
duration: effectiveFocusEffectDuration,
child: buildToggleable(
mouseCursor: effectiveMouseCursor,
focusNode: widget.focusNode,
autofocus: widget.autofocus,
size: size,
painter: _painter
..position = position
..activeColor = effectiveActiveColor
..inactiveColor = effectiveInactiveColor
..checkColor = effectiveCheckColor
..value = value
..previousValue = _previousValue
..shape = SmoothRectangleBorder(borderRadius: effectiveBorderRadius.smoothBorderRadius)
..side = _resolveSide(BorderSide(color: effectiveBorderColor)),
),
),
),
),
Expand Down
Loading

0 comments on commit 906386c

Please sign in to comment.