Search code examples
flutterpadding

Padding inside the button


Is there any way to use padding as a parameter inside the primary button? while doing so i don't have to use the padding widget anymore and i can choose the padding: EdgeInsets.symmetric( horizontal:small/medium or large),

Here is my button class:

class SdPrimaryButton extends ElevatedButton {
SdPrimaryButton({
required String title,
required VoidCallback? onPressed,
// required Padding padding,
VoidCallback? onLongPress,
Key? key,
}) : super(
      key: key,
      onPressed: onPressed,
      onLongPress: onLongPress,
      style: ElevatedButton.styleFrom(),
      child: Text(title),
    );
}

Here is the default case: As you can see every time when I have to use my button I have to define the padding widget to achieve the required result. Is there any way that I can call my button and define the padding inside the button and achieve the same result by setting the padding value as small medium or large?

  Padding(
          padding: const EdgeInsets.symmetric(
          horizontal: medium * 2),
          child: SizedBox(
          width: double.infinity,
           child: SdPrimaryButton(
                  title: appLocalizations.labelContinue,
                  onPressed: () {
                              if (widget.checkValidation()) {
                                widget.pageController!.nextPage(
                                    duration:
                                        const Duration(milliseconds: 500),
                                    curve: Curves.easeInOut);
                                _scrollToTop();
                              }
                            },
                          ),
                        ),
                      ),

Solution

  • You can pass padding style: ElevatedButton.styleFrom(

    enum PaddingState {
      small(10),
      medium(20),
      large(30);
    
      final double value;
      const PaddingState(this.value);
    }
    
    class SdPrimaryButton extends ElevatedButton {
      SdPrimaryButton({
        required String title,
        required VoidCallback? onPressed,
        VoidCallback? onLongPress,
        PaddingState padding = PaddingState.medium,
        Key? key,
      }) : super(
              key: key,
              onPressed: onPressed,
              onLongPress: onLongPress,
              style: ElevatedButton.styleFrom(
                  padding: EdgeInsets.symmetric(horizontal: padding.value * 2)),
              child: Text(title),
            );
    }