Home > Mobile >  How to create a gradient button in flutter
How to create a gradient button in flutter

Time:12-27

I am trying to create a button similar to this one, I don't have exact colors so using yellow and black.

Want this

enter image description here

My Code Output

enter image description here

here is my code:

class CustomButton extends StatelessWidget {
  final String? text;
  double width;
  final Function()? onPressed;
  CustomButton({this.width = 0.8, this.text, this.onPressed});
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPressed,
      child: Container(
        child: Center(
            child: CustomTextWidget(
          text: text!,
          textColor: AppColors.BLACK_COLOR,
          fontWeight: FontWeight.bold,
          fontSize: 1.1,
        )),
        height: ScreenSize.heightSize * 0.08,
        width: width.sw,
        decoration: BoxDecoration(
            gradient: const LinearGradient(
              colors: [Colors.yellow, Colors.black],
              begin: Alignment.topRight,
              end: Alignment.topRight,
              stops: [0.7, 0.8],
              tileMode: TileMode.repeated,
            ),
            borderRadius: BorderRadius.circular(4)),
      ),
    );
  }
}

Kindly help how to do this.

CodePudding user response:

Try below code hope its help to you

GestureDetector(
  onTap: () {},
  child: Container(
    alignment: Alignment.center,
    height: 44.0,
    width: 100,
    decoration: const BoxDecoration(
      gradient: LinearGradient(
        colors: [
          Colors.yellow,
          Colors.black,
        ],
        begin: FractionalOffset.bottomCenter,
        end: FractionalOffset.topCenter,
      ),
    ),
    child: const Text('SIGN UP'),
  ),
),

Result-> image

  • Related