Create a rounded button / button with border-radius in Flutter
#1
I'm working on a Flutter project where I need to create a button that has rounded corners. I am aware that Flutter provides a wide variety of widgets that can be customized extensively, but I'm having a bit of trouble figuring out the best way to create a rounded button. I want the button to be reusable throughout my application, so I'm also looking for a solution that allows for flexibility in terms of the button's dimensions and the degree of the roundness.
For some context, here's what I currently have:

Code:
class MyButton extends StatelessWidget {
    final String text;
    final VoidCallback onPressed;
    MyButton({
        required this.text,
        required this.onPressed
    });
    @override
    Widget build(BuildContext context) {
        return ElevatedButton(
            onPressed: onPressed,
            child: Text(text),
            // The button's style needs to be defined here to make rounded corners
        );
    }
}

As you can see, I have a basic button that takes in text and an onPressed callback, but I haven't been able to get the rounded corners the way I want them. What is the best practice for achieving this in Flutter? I assume there's a way to do this using the `style` property of the `ElevatedButton`, but I'm not sure about the specifics.
Reply
#2
Indeed, you are on the right track thinking about using the `style` property. You can customize the `ElevatedButton` by creating a `ButtonStyle` object and setting its `shape` property. The `shape` property takes a `ShapeBorder`, and for rounded corners, you can use `RoundedRectangleBorder`. Here's how you can define the rounded corners:

Code:
),
);

Then you can apply this `roundedButtonStyle` to your `ElevatedButton` like this:

Code:
ElevatedButton(
    onPressed: onPressed,
    child: Text(text),
    style: roundedButtonStyle,
)

If you want even more customization, for instance, a gradient background, that would involve more detailed styling. But for just rounded corners, the above method should suffice.
Reply
#3
That definitely clears things up. I appreciate the succinct example. I'll implement it as suggested and make the button style flexible for different use cases within my application.
Here's the revised `MyButton` class with the rounded corners included:

Code:
class MyButton extends StatelessWidget {
    final String text;
    final VoidCallback onPressed;
    final double borderRadius;
    MyButton({
        required this.text,
        required this.onPressed,
        this.borderRadius = 30.0,
    });
    @override
    Widget build(BuildContext context) {
        ButtonStyle roundedButtonStyle = ElevatedButton.styleFrom(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(borderRadius),
            ),
        );
        return ElevatedButton(
            onPressed: onPressed,
            child: Text(text),
            style: roundedButtonStyle,
        );
    }
}

With this class, you can now create a `MyButton` instance with customizable rounded corners using the `borderRadius` parameter. This should now be ready to be used throughout the application.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)