0

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();
                              }
                            },
                          ),
                        ),
                      ),
Kathrin
  • 15
  • 2

1 Answers1

0

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),
        );
}
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • I want to change the horizontal value as well like medium/large or small. with your approach it always give me horizontal padding medium* 2 i can not change it whenever i call the SdPrimaryButton – Kathrin Aug 15 '22 at 14:54
  • Would you like to have enum ? or just pass const value on padding, try update answer – Md. Yeasin Sheikh Aug 15 '22 at 14:55
  • Yes something like enum for small medium and large value for horizontal padding. for example if i want to use two SdPrimaryButton for first one i want to set the horizontal padding medium and for 2nd one i want to use it large/small – Kathrin Aug 15 '22 at 14:56
  • Ok try with current `enhanced-enums` – Md. Yeasin Sheikh Aug 15 '22 at 15:01
  • is there any way that i can set this padding for different screen sizes let's say for mobile padding should be small for the computer it should be large and for tablets, it should be medium. i don't have to change it every time inside the SdPrimary button where ever i am using it – Kathrin Aug 16 '22 at 16:18
  • you can [get the platform](https://stackoverflow.com/q/45924474/10157127) but I prefer using LayoutBuilder at top level and based on it I handle the UI. – Md. Yeasin Sheikh Aug 16 '22 at 16:20