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

Bug: Can't override button style shape #224

Closed
lukengda opened this issue Jan 28, 2025 · 1 comment
Closed

Bug: Can't override button style shape #224

lukengda opened this issue Jan 28, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@lukengda
Copy link
Contributor

It is currently impossible to change the shape of intro_buttons using baseBtnStyle or nextStyle (same applies for all style attributes).

Describe the bug

This is due to the fact that ButtonStyle.merge from Flutter takes precedence on the caller objects non-null values over the style argument and the merge order in the intro_button.dart implementation: https://github.com/Pyozer/introduction_screen/blob/master/lib/src/ui/intro_button.dart#L26C1-L30C26

          style: TextButton.styleFrom(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8.0),
            ),
          ).merge(style),

To Reproduce
Steps to reproduce the behavior:

  1. Create an IntroductionScreen instance with the following BaseBtnStyle:
       baseBtnStyle: TextButton.styleFrom(
         foregroundColor: Colors.red,
         shape: RoundedRectangleBorder(
           borderRadius: BorderRadius.circular(8.0),
           side: BorderSide(),
         ),
       ),
  2. See that the text color has changed to red but the button has no border applied.

Expected behavior

I expect to be able to override the shape of buttons using ButtonStyle.

I see two options in solving this:

a) the most backwards-compatible solution would be to still merge the styles but change the order so that the custom style overrules the default style:

--- a/lib/src/ui/intro_button.dart
+++ b/lib/src/ui/intro_button.dart
@@ -16,6 +16,11 @@ class IntroButton extends StatelessWidget {
 
   @override
   Widget build(BuildContext context) {
+    final ButtonStyle defaultStyle = TextButton.styleFrom(
+      shape: RoundedRectangleBorder(
+        borderRadius: BorderRadius.circular(8.0),
+      ),
+    );
     return MergeSemantics(
       child: Semantics(
         label: semanticLabel,
@@ -23,11 +28,7 @@ class IntroButton extends StatelessWidget {
         child: TextButton(
           onPressed: onPressed,
           child: child,
-          style: TextButton.styleFrom(
-            shape: RoundedRectangleBorder(
-              borderRadius: BorderRadius.circular(8.0),
-            ),
-          ).merge(style),
+          style: style?.merge(defaultStyle) ?? defaultStyle,
         ),
       ),
     );

b) the more straight-forward solution would be to only apply the default shape if no style is provided:

@@ -23,11 +23,12 @@ class IntroButton extends StatelessWidget {
         child: TextButton(
           onPressed: onPressed,
           child: child,
-          style: TextButton.styleFrom(
-            shape: RoundedRectangleBorder(
-              borderRadius: BorderRadius.circular(8.0),
-            ),
-          ).merge(style),
+          style: style ??
+              TextButton.styleFrom(
+                shape: RoundedRectangleBorder(
+                  borderRadius: BorderRadius.circular(8.0),
+                ),
+              ),
         ),
       ),
     );

Screenshots

Here you can see screenshots of the example how to reproduce and that the border is applied with either of the proposed changes:

Current:
Image

With proposed changes applied:
Image

Smartphone (please complete the following information):

  • Device: should apply to all devices, shown on Pixel 6 or iPhone 15
  • OS: n/a
  • Version 3.1.14

Additional context
Add any other context about the problem here.

@lukengda lukengda added the bug Something isn't working label Jan 28, 2025
lukengda added a commit to lukengda/introduction_screen that referenced this issue Feb 11, 2025
TextButton style merge is implemented in a way that the original object takes precedence over the argument.
We therefore switch the merge orientation to ensure that it is possible to  override every style.

Closes Pyozer#224
@Pyozer Pyozer closed this as completed in 6858973 Feb 12, 2025
@Pyozer
Copy link
Owner

Pyozer commented Feb 12, 2025

Release 3.1.15 available ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants