Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Token was not being resolved correctly #525

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/mix/lib/src/core/factory/mix_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class MixData with Diagnosticable {
// /// Merges this [MixData] with another, prioritizing this instance's properties.
MixData merge(MixData other) {
return MixData._(
resolver: _tokenResolver,
resolver: other._tokenResolver,
attributes: _attributes.merge(other._attributes),
animation: other.animation ?? animation,
);
Expand Down
89 changes: 89 additions & 0 deletions packages/mix/test/src/core/factory/mix_data_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mix/mix.dart';

import '../../../helpers/testing_utils.dart';

// Define theme color tokens
const _surface = ColorToken('surface');
const _onSurface = ColorToken('onSurface');

// Light theme
final _lightTheme = MixThemeData(
colors: {
_surface: const Color(0xFF000000),
_onSurface: const Color(0xFFFFFFFF),
},
);

// Dark theme
final _darkTheme = MixThemeData(
colors: {
_surface: const Color(0xFFFFFFFF),
_onSurface: const Color(0xFF000000),
},
);

// Widget wrapper that allows toggling between themes
class _ThemeToggleWrapper extends StatefulWidget {
const _ThemeToggleWrapper({required this.child});

final Widget child;

@override
State<_ThemeToggleWrapper> createState() => _ThemeToggleWrapperState();
}

class _ThemeToggleWrapperState extends State<_ThemeToggleWrapper> {
bool isDark = false;

void toggleTheme() {
setState(() {
isDark = !isDark;
});
}

@override
Widget build(BuildContext context) {
return MixTheme(
data: isDark ? _darkTheme : _lightTheme,
child: widget.child,
);
}
}

// Sample widget that uses theme colors
class _ThemedBox extends StatelessWidget {
const _ThemedBox();

@override
Widget build(BuildContext context) {
return Box(
style: Style($box.color.ref(_surface)),
child: StyledText('test', style: Style($text.color.ref(_onSurface))),
);
}
}

void main() {
testWidgets('should update colors when theme changes', (tester) async {
await tester.pumpMaterialApp(
const _ThemeToggleWrapper(child: _ThemedBox()),
);

final themeToggle = tester.state<_ThemeToggleWrapperState>(
find.byType(_ThemeToggleWrapper),
);

Text text = tester.widget<Text>(find.byType(Text));
expect(themeToggle.isDark, isFalse);
expect(text.style?.color, const Color(0xFFFFFFFF));

themeToggle.toggleTheme();
await tester.pump();

text = tester.widget<Text>(find.byType(Text));
expect(themeToggle.isDark, isTrue);
expect(text.style?.color, const Color(0xFF000000));
});
}
Loading