Skip to content
Draft
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
84 changes: 84 additions & 0 deletions test/button_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_test/flutter_test.dart';

TextStyle _renderedTextStyle(WidgetTester tester, Finder buttonFinder) {
final richTextFinder = find.descendant(
of: buttonFinder,
matching: find.byType(RichText),
);
return tester.widget<RichText>(richTextFinder).text.style!;
}
Comment on lines +4 to +10
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The _renderedTextStyle helper is fragile because it assumes exactly one RichText descendant exists within the button. In Flutter, widgets like Icon also use RichText internally. If a button were to include both an icon and text, this helper would fail with a 'too many elements' error. While it works for the current test cases, consider making the finder more specific to target the intended text label (e.g., by finding the Text widget first) or using .first if you only care about the primary style.


void main() {
testWidgets('Test constraints', (tester) async {
await tester.pumpWidget(
Expand Down Expand Up @@ -43,4 +51,80 @@ void main() {
expect(innerBox.constraints.maxHeight, lessThanOrEqualTo(200));
expect(innerBox.size, Size.zero);
});

testWidgets(
'Button and FilledButton use default foreground when themed styles omit '
'textStyle',
(tester) async {
await tester.pumpWidget(
FluentApp(
theme: FluentThemeData(),
home: Column(
mainAxisSize: MainAxisSize.min,
children: [
Button(onPressed: () {}, child: const Text('Press me')),
FilledButton(onPressed: () {}, child: const Text('Press Me')),
],
),
),
);

final buttonContext = tester.element(find.byType(Button));
final filledButtonContext = tester.element(find.byType(FilledButton));
final theme = FluentTheme.of(buttonContext);

final expectedButtonColor = ButtonThemeData.buttonForegroundColor(
buttonContext,
{},
);
final expectedFilledButtonColor = FilledButton.foregroundColor(theme, {});

final renderedButtonColor = _renderedTextStyle(
tester,
find.byType(Button),
).color;
final renderedFilledButtonColor = _renderedTextStyle(
tester,
find.byType(FilledButton),
).color;

expect(renderedButtonColor, expectedButtonColor);
expect(renderedFilledButtonColor, expectedFilledButtonColor);

// Keep the second context in use to ensure both are bound to the same theme.
expect(FluentTheme.of(filledButtonContext), same(theme));
},
);

testWidgets('Button uses inherited TextStyle color', (tester) async {
await tester.pumpWidget(
FluentApp(
theme: FluentThemeData(
buttonTheme: ButtonThemeData(
defaultButtonStyle: ButtonStyle(
textStyle: WidgetStatePropertyAll(TextStyle(color: Colors.blue)),
),
filledButtonStyle: ButtonStyle(
textStyle: WidgetStatePropertyAll(
TextStyle(color: Colors.yellow),
),
),
),
),
home: Column(
mainAxisSize: MainAxisSize.min,
children: [
Button(onPressed: () {}, child: const Text('Press me')),
FilledButton(onPressed: () {}, child: const Text('Press Me')),
],
),
),
);

expect(_renderedTextStyle(tester, find.byType(Button)).color, Colors.blue);
expect(
_renderedTextStyle(tester, find.byType(FilledButton)).color,
Colors.yellow,
);
});
}
Loading