Skip to content

Commit

Permalink
feat: add error state to MixWidgetState
Browse files Browse the repository at this point in the history
  • Loading branch information
tilucasoli committed Sep 26, 2024
1 parent a7a0fcc commit d08bde4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 13 deletions.
2 changes: 2 additions & 0 deletions examples/todo_list/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/

# IntelliJ related
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MixWidgetStateBuilder extends StatelessWidget {
pressed: controller.pressed,
dragged: controller.dragged,
selected: controller.selected,
error: controller.error,
longPressed: controller.longPressed,
child: Builder(builder: builder),
);
Expand All @@ -42,6 +43,7 @@ class MixWidgetStateModel extends InheritedModel<MixWidgetState> {
required this.pressed,
required this.dragged,
required this.selected,
required this.error,
required this.longPressed,
required super.child,
});
Expand Down Expand Up @@ -70,16 +72,18 @@ class MixWidgetStateModel extends InheritedModel<MixWidgetState> {
MixWidgetState.dragged => model.dragged,
MixWidgetState.selected => model.selected,
MixWidgetState.longPressed => model.longPressed,
MixWidgetState.error => model.error,
};
}

final bool disabled;
final bool hovered;
final bool focused;
final bool pressed;
final bool longPressed;
final bool dragged;
final bool selected;
final bool longPressed;
final bool error;

@override
bool updateShouldNotify(MixWidgetStateModel oldWidget) {
Expand All @@ -89,7 +93,8 @@ class MixWidgetStateModel extends InheritedModel<MixWidgetState> {
oldWidget.pressed != pressed ||
oldWidget.dragged != dragged ||
oldWidget.selected != selected ||
oldWidget.longPressed != longPressed;
oldWidget.longPressed != longPressed ||
oldWidget.error != error;
}

@override
Expand All @@ -110,6 +115,7 @@ class MixWidgetStateModel extends InheritedModel<MixWidgetState> {
oldWidget.selected != selected &&
dependencies.contains(MixWidgetState.selected) ||
oldWidget.longPressed != longPressed &&
dependencies.contains(MixWidgetState.longPressed);
dependencies.contains(MixWidgetState.longPressed) ||
oldWidget.error != error && dependencies.contains(MixWidgetState.error);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum MixWidgetState {
dragged,
selected,
disabled,
error,
longPressed;

static const of = MixWidgetStateModel.of;
Expand Down Expand Up @@ -49,6 +50,9 @@ class MixWidgetStateController extends ChangeNotifier {
/// Whether the widget is currently in the selected state.
bool get selected => value.contains(MixWidgetState.selected);

/// Whether the widget is currently in the error state.
bool get error => value.contains(MixWidgetState.error);

/// Whether the widget is currently being long-pressed.
bool get longPressed => value.contains(MixWidgetState.longPressed);

Expand All @@ -70,6 +74,9 @@ class MixWidgetStateController extends ChangeNotifier {
/// Sets whether the widget is in the selected state.
set selected(bool value) => update(MixWidgetState.selected, value);

/// Sets whether the widget is in the selected state.
set error(bool value) => update(MixWidgetState.error, value);

/// Sets whether the widget is being long-pressed.
set longPressed(bool value) => update(MixWidgetState.longPressed, value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class OnContextVariantUtility {
final selected = const OnSelectedVariant();
final unselected = const OnNotVariant(OnSelectedVariant());
final dragged = const OnDraggedVariant();
final error = const OnErrorVariant();

/// Creates an [OnNotVariant] with the specified [variant].
///
Expand Down
5 changes: 5 additions & 0 deletions packages/mix/lib/src/variants/widget_state_variant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ class OnSelectedVariant extends _ToggleMixStateVariant {
class OnDraggedVariant extends _ToggleMixStateVariant {
const OnDraggedVariant() : super(MixWidgetState.dragged);
}

/// Applies styles when the widget is error.
class OnErrorVariant extends _ToggleMixStateVariant {
const OnErrorVariant() : super(MixWidgetState.error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ void main() {
controller.dragged = true;
controller.selected = true;
controller.longPressed = true;
controller.error = true;

await tester.pumpWidget(
MaterialApp(
Expand All @@ -137,6 +138,7 @@ void main() {
dragged: controller.dragged,
selected: controller.selected,
longPressed: controller.longPressed,
error: controller.error,
child: Container(),
),
),
Expand All @@ -151,6 +153,7 @@ void main() {
expect(foundModel.dragged, isTrue);
expect(foundModel.selected, isTrue);
expect(foundModel.longPressed, isTrue);
expect(foundModel.error, isTrue);
});

testWidgets('hasStateOf returns if state is set', (tester) async {
Expand All @@ -164,20 +167,23 @@ void main() {
dragged: false,
selected: false,
longPressed: false,
error: false,
child: Builder(
builder: (context) {
expect(
MixWidgetStateModel.hasStateOf(
context,
MixWidgetState.disabled,
),
isTrue);
MixWidgetStateModel.hasStateOf(
context,
MixWidgetState.disabled,
),
isTrue,
);
expect(
MixWidgetStateModel.hasStateOf(
context,
MixWidgetState.hovered,
),
isFalse);
MixWidgetStateModel.hasStateOf(
context,
MixWidgetState.hovered,
),
isFalse,
);
return Container();
},
),
Expand All @@ -195,6 +201,7 @@ void main() {
dragged: false,
selected: false,
longPressed: false,
error: false,
child: Container(),
);
final newModel = MixWidgetStateModel(
Expand All @@ -205,6 +212,7 @@ void main() {
dragged: false,
selected: false,
longPressed: false,
error: false,
child: Container(),
);

Expand All @@ -220,6 +228,7 @@ void main() {
dragged: false,
selected: false,
longPressed: false,
error: false,
child: Container(),
);
final newModel = MixWidgetStateModel(
Expand All @@ -230,6 +239,7 @@ void main() {
dragged: false,
selected: false,
longPressed: false,
error: false,
child: Container(),
);

Expand Down

0 comments on commit d08bde4

Please sign in to comment.