How to make Elevated Button with Gradient background? - flutter

I am trying to create an Elevated button with gradient background, But it provides some parameters that do not fit it well, and May you know that after Flutter 2.0 version most of the Button classes have been deprecated such as Raised Button, Flat Button, ... etc
ElevatedButton(
child: Text('Woolha.com'),
style: ElevatedButton.styleFrom(
primary: Colors.teal,
onPrimary: Colors.white,
onSurface: Colors.grey,
),
onPressed: () {
print('Pressed');
},
)
Is there anyway to create ElevatedButton with gradient background?

Screenshot (Null safe)
Create this class (customizable)
class MyElevatedButton extends StatelessWidget {
final BorderRadiusGeometry? borderRadius;
final double? width;
final double height;
final Gradient gradient;
final VoidCallback? onPressed;
final Widget child;
const MyElevatedButton({
Key? key,
required this.onPressed,
required this.child,
this.borderRadius,
this.width,
this.height = 44.0,
this.gradient = const LinearGradient(colors: [Colors.cyan, Colors.indigo]),
}) : super(key: key);
#override
Widget build(BuildContext context) {
final borderRadius = this.borderRadius ?? BorderRadius.circular(0);
return Container(
width: width,
height: height,
decoration: BoxDecoration(
gradient: gradient,
borderRadius: borderRadius,
),
child: ElevatedButton(
onPressed: onPressed,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.transparent,
shadowColor: Colors.transparent,
shape: RoundedRectangleBorder(borderRadius: borderRadius),
),
child: child,
),
);
}
}
Usage:
Use it like a regular ElevatedButton:
MyElevatedButton(
width: double.infinity,
onPressed: () {},
borderRadius: BorderRadius.circular(20),
child: Text('SIGN IN'),
)

import 'package:flutter/material.dart';
class RoundedButtonWidget extends StatelessWidget {
final String buttonText;
final double width;
final Function onpressed;
RoundedButtonWidget({
required this.buttonText,
required this.width,
required this.onpressed,
});
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10.0),
child: Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black26, offset: Offset(0, 4), blurRadius: 5.0)
],
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
stops: [0.0, 1.0],
colors: [
Colors.deepPurple.shade400,
Colors.deepPurple.shade200,
],
),
color: Colors.deepPurple.shade300,
borderRadius: BorderRadius.circular(20),
),
child: ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
),
minimumSize: MaterialStateProperty.all(Size(width, 50)),
backgroundColor:
MaterialStateProperty.all(Colors.transparent),
// elevation: MaterialStateProperty.all(3),
shadowColor:
MaterialStateProperty.all(Colors.transparent),
),
onPressed: () {
onpressed();
},
child: Padding(
padding: const EdgeInsets.only(
top: 10,
bottom: 10,
),
child: Text(
buttonText,
style: TextStyle(
fontSize: 18,
// fontWeight: FontWeight.w700,
color: Colors.white,
),
),
),
),
),
);
}
}

Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(6.0),
gradient: LinearGradient(
begin: Alignment(-0.95, 0.0),
end: Alignment(1.0, 0.0),
colors: [const Color(0xff667eea), const Color(0xff64b6ff)],
stops: [0.0, 1.0],
),
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(primary: Colors.transparent,
onSurface: Colors.transparent,
shadowColor: Colors.transparent,),
onPressed: (){},
child: Center(
child: Text(
'Log in',
style: TextStyle(
fontFamily: textFontFamilySegoeUI,
fontSize: 16,
color: const Color(0xffffffff),
letterSpacing: -0.3858822937011719,
),
textAlign: TextAlign.center,
),
),
),
),

Container(
width: 1120.w,
height: 180.h,
margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xFF0379C6), Color(0xFF1298EF)]),
borderRadius: BorderRadius.circular(25.r),
boxShadow: <BoxShadow>[
BoxShadow(
color: Color.fromRGBO(16, 142, 233, 0.57),
blurRadius: 13)
]),
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
Colors.transparent),
),
onPressed: () => setState(() {
// _launched = _makePhoneCall('tel:$_phone');
}),
child: Text(
'Submit',
style:
TextStyle(color: Colors.white, fontSize: 64.sp),
),
),
)

botao
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Priscila Dev'),
),
body: Center(
child: Container(
width: 300,
height: 100,
decoration: const ShapeDecoration(
shape: StadiumBorder(),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.blue, Colors.orange, Colors.green],
),
),
child: MaterialButton(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
shape: const StadiumBorder(),
child: const Text(
'ENCERRAR[enter image description here][1]',
style: TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: () {
print('Hello!');
},
),
)),
);
}

Try this
Container(
padding: EdgeInsets.fromLTRB(10.0, 6.0, 10.0, 6.0),
decoration: BoxDecoration(
gradient:
LinearGradient(colors: [Color(0xff3b8594), Color(0xff59a6b6)]),
borderRadius: BorderRadius.circular(25.0)),
child: Text(
'View more',
style: TextStyle(color: Colors.white, fontSize: 16.0),
),
)
Result

Related

How to overwrite color of ElevatedButton with LinearGradient?

I am trying to do ElevatedButton with LinearGradient, I am giving it to Container around my button, but it's not affecting it.
I tried to set backgroundColor of button to transparent, but it's not looking how it should.
This is my code:
Container(
margin: EdgeInsets.symmetric(horizontal: size.width * 0.15, vertical: size.height * 0.03),
decoration: BoxDecoration(
gradient: LinearGradient(colors: [Color(0xffFF2973), Color(0xffFF6ABD)]),
borderRadius: BorderRadius.circular(10)
),
width: size.width * 0.7,
height: 45,
child: ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10)
)
),
backgroundColor: MaterialStateProperty.all(Colors.transparent),
),
On left picture this is how its looking now, below its how it should look
On right picture this is how its looking without backgroundColor
Try below code hope its helpful to you.
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Colors.pink,
Colors.red,
Colors.orange,
],
),
),
child: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
primary: Colors.transparent,
shadowColor: Colors.transparent,
minimumSize: Size (50,100),
),
child: Text('Elevated Button'),
),
),
Result->
InkWell(
onTap: () {},
child: Container(
margin: EdgeInsets.symmetric(
horizontal: MediaQuery.of(context).size.width * 0.15,
vertical: MediaQuery.of(context).size.height * 0.03,
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.centerLeft,
end: Alignment.centerRight,
colors: [
Colors.green.withOpacity(0.5),
Colors.black.withOpacity(0.5)
]),
borderRadius: BorderRadius.circular(10)),
width: MediaQuery.of(context).size.width * 0.7,
height: 45,
alignment: Alignment.center,
child: const Text('Hello Text'),
),
),
class Example extends StatefulWidget {
const Example({Key? key}) : super(key: key);
#override
State<StatefulWidget> createState() {
return _Examplestate();
}
}
class _Examplestate extends State<Example> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Stateful Widget'),
),
body: Center(
child: RadiantGradientMask(
gradient:const LinearGradient(
colors: [
Colors.pink,
Colors.red,
Colors.orange,
],
),
child: ElevatedButton(
style: ElevatedButton.styleFrom(primary: Colors.white),
onPressed: () {},
child: const Text("Hello",style: TextStyle(color: Colors.black),),
),
),
));
}
}
class RadiantGradientMask extends StatelessWidget {
const RadiantGradientMask({
Key? key,
required this.child,
required this.gradient,
}) : super(key: key);
final Widget child;
final Gradient gradient;
#override
Widget build(BuildContext context) {
return ShaderMask(
shaderCallback: (Rect bounds) {
return gradient.createShader(bounds);
},
child: child,
);
}
}
Result ->

How to get shadow outside of a container in flutter

I want to add an outer shadow into a button, but the shadow is filling the whole button.
Following is how I want the button to look.
This is how it actually looks:
Following is the code:
Container(
width: (width != null ? width / 2 : 300 / 2) - 2,
height: 35,
decoration: BoxDecoration(
boxShadow: selectedState == "EveryWeek"
? customThemeProvider.isLuminescenceTheme
? [
BoxShadow(
color: Color(0xffCEA6F8),
blurRadius: 5,
spreadRadius: 0.02,
offset: Offset(0.1, 0.1)),
BoxShadow(
color: Color(0xff6E2DF9),
blurRadius: 5,
spreadRadius: 0.2,
offset: Offset(0.1, 0.1)),
]
: null
: customThemeProvider.isLuminescenceTheme
? null
: null,
...
How do I get the outer shadow?
I think this is matching pretty well. You can always adjust the details:
Container(
padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.deepPurpleAccent,
spreadRadius: 2,
offset: Offset(0, 0),
),
],
color: Colors.deepPurple[900],
),
child: Text(
'Monthly',
style: TextStyle(color: Colors.white),
),
),
Try below code hope its help to you.
Container(
height: 50,
width: 150,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Color(0xffCEA6F8),
Color(0xff6E2DF9),
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
child: Container(
alignment: Alignment.center,
child: Text(
'Monthly',
),
),
),
),
Your Result screen->
Full Example:
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 50,
width: 150,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(
colors: [
Color(0xffCEA6F8),
Color(0xff6E2DF9),
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
child: Container(
alignment: Alignment.center,
child: Text(
'Monthly',
),
),
),
);
}
}
Refer my answer here also
You can test your code on Dartpad
If by shadow you don't mean the shadow made by elevation, you can create the style you want using gradient but it needs a little trick, because you should wrap a container in another one. Here is a sample:
import 'package:flutter/material.dart';
class TestScreen extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: SafeArea(
child: Center(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
const Color(0xffa983f5),
const Color(0xff6E2DF9),
]),
borderRadius: BorderRadius.only(
topRight: Radius.circular(8),
bottomRight: Radius.circular(8),
),
),
child: Padding(
padding: const EdgeInsets.all(2.0),
child: Container(
child: Padding(
padding: const EdgeInsets.symmetric(
vertical: 16.0,
horizontal: 64,
),
child: Text(
'Monthly',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16,
),
textAlign: TextAlign.center,
),
),
decoration: BoxDecoration(
color: const Color(0xFF0E0132), //Colors.deepPurple.shade900,
borderRadius: BorderRadius.only(
topRight: Radius.circular(8),
bottomRight: Radius.circular(8),
),
),
),
),
),
)));
}
}

Flutter - How to get faded color border along a circle?

See how the edges are faded and merged with the color of the button? How do you get something like that in flutter?
I have the button, don't know how to get that effect.
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(top: 30),
child: Column(
children: [
//OUTER CIRCLE (BUTTON INSIDE)
Container(
height: 275,
width: 275,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(150),
border: Border.all(
width: 3,
color: Colors.grey.withOpacity(0.6),
style: BorderStyle.solid,
),
),
child: Padding(
padding: const EdgeInsets.all(40),
//SHADOW (Button inside)
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: const Color(0xfffd194b).withOpacity(0.7),
spreadRadius: 10,
blurRadius: 40,
offset: const Offset(0, 25),
),
],
),
//BUTTON
child: RecordButtonWithText(
buttonText: _buttonText,
startStopRecording: startStopRecording,
),
),
),
),
],
),
);
class RecordButtonWithText extends StatelessWidget {
const RecordButtonWithText({
Key? key,
required String buttonText,
required this.startStopRecording,
}) : _buttonText = buttonText,
super(key: key);
final String _buttonText;
final VoidCallback startStopRecording;
#override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
startStopRecording();
},
style: ElevatedButton.styleFrom(
elevation: 10,
padding: const EdgeInsets.all(30),
shape: const CircleBorder(),
shadowColor: const Color(0xfffd194b),
primary: const Color(0xfffd194b),
),
child: Text(
_buttonText.toUpperCase(),
style: const TextStyle(fontSize: 22, letterSpacing: 3),
),
);
}
}
If the you don't have to use the "ElevatedButton" widget, you can achieve what you want like that:
class RecordButtonWithText extends StatelessWidget {
final String _buttonText;
final VoidCallback startStopRecording;
final EdgeInsets padding;
final double size;
const RecordButtonWithText({
Key? key,
required String buttonText,
required this.startStopRecording,
this.padding = const EdgeInsets.all(30.0),
this.size = 275.0,
}) : _buttonText = buttonText,
super(key: key);
#override
Widget build(BuildContext context) {
return Container(
height: size,
width: size,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(
width: 2,
color: Colors.grey.withOpacity(0.4),
style: BorderStyle.solid,
),
),
child: Padding(
padding: EdgeInsets.all(size * 0.145),
//SHADOW (Button inside)
child: Container(
decoration: BoxDecoration(
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: const Color(0xfffd194b).withOpacity(0.7),
spreadRadius: size * 0.03,
blurRadius: size * 0.145,
offset: Offset(0, size * 0.09),
),
],
),
//BUTTON
child: InkWell(
borderRadius: BorderRadius.circular(size / 2),
onTap: () {
startStopRecording();
},
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.all(size * 0.1),
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.black,
gradient: RadialGradient(colors: [
const Color(0xfffd194b),
const Color(0xfffd194b),
const Color(0xfffd194b).withOpacity(0.5),
], stops: const [
0,
0.8,
1
]),
),
child: Text(
_buttonText.toUpperCase(),
style: TextStyle(
color: Colors.white,
fontSize: size * 0.08,
),
),
),
),
),
),
);
}
}

I want to add bookmarks or favorites feature in flutter

The code below is my button part code.
When this button is clicked, I want to display the color of the border of the bookmarked buttons on the "Page Selection Page (code below)" to change color.
Actually the most important part is using shared_preferences: ^2.0.7 to make it stored locally.
I've seen and tried several examples, but I'm struggling on the 4th day.
bool recognizing = false;
...
actions: <Widget>[
IconButton(
onPressed: recognizing ?
Function for Remove from Favorites : function to add to favorites,
icon: recognizing
? Icon(Icons.bookmark_rounded, color: Colors.yellow[800], size: 30) //그대로일때
: Icon(Icons.bookmark_add_outlined, color: Colors.yellow[800],size: 30),
),
],
Below is the button code. In the previous "Page Selection Page", call and use the code below as child:LearnLevelButton().
class LearnLevelButton extends StatelessWidget {
LearnLevelButton({
Key ?key,
this.text = '',
this.width = 80.0,
this.height = 80.0,
this.borderRadius = 50.0,
required this.onTap,
}) : super(key: key);
final String text;
final VoidCallback onTap;
final double width;
final double height;
final double borderRadius;
#override
Widget build(BuildContext context) {
var gradient = LinearGradient(
begin: Alignment.topCenter, // new
end: Alignment.bottomCenter, // new
colors: [
//Color(0xff9cbbf6),
//Color(0xff9cbbf6),linkwell
Color(0xff7ba6f9),
Color(0xff7ba6f9),
],
);
return InkWell(
onTap: onTap,
child: Center(
child: Padding(
padding: const EdgeInsets.only(
top: 20.0,
bottom: 20.0,
left: 22.0,
right: 22.0,
),
child: Container(
padding: const EdgeInsets.all(3.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(borderRadius),
border: Border.all(
width: 0.3,
color: Colors.black38,
),
boxShadow: [
BoxShadow(
blurRadius: 10.0,
offset: Offset(5.0, 5.0),
color: Color.fromRGBO(0, 0, 0, 0.3),
),
],
gradient: gradient,
),
width: width,
height: height,
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(borderRadius),
border: Border.all(
width: 0.3,
color: Colors.black26,
),
boxShadow: [
BoxShadow(
blurRadius: 1.0,
offset: Offset(1.0, 1.5),
color: Color.fromRGBO(0, 0, 0, 0.3),
),
],
gradient: gradient,
),
child: Center(
child: Text(
text,
style: TextStyle(
color: Colors.white,
fontSize: 20.0, fontWeight: FontWeight.bold,
),
),
),
),
),
),
),
);
}
}

How to reduce repetitive code for buttons in flutter?

I have more than 10 buttons that I need to display in my flutter. Here's the sample code for my 2 repetitive buttons:
Padding(
padding: const EdgeInsets.all(8.0),
child: Material(
clipBehavior: Clip.antiAlias,
shape: const StadiumBorder(),
child: Ink(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.black,
Colors.grey,
Colors.white,
],
),
),
width: double.infinity,
height: 60,
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => PrivacyRoute()),
);
},
child: ListTile(
leading: Icon(
Icons.menu_book,
color: Colors.white,
),
title: Text('Bahasa Melayu',
style: TextStyle(
color: Colors.white,
fontSize: 20,
)),
),
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Material(
clipBehavior: Clip.antiAlias,
shape: const StadiumBorder(),
child: Ink(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [
Colors.black,
Colors.grey,
Colors.white,
],
),
),
width: double.infinity,
height: 60,
child: InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => PrivacyRoute()),
);
},
child: ListTile(
leading: Icon(
Icons.menu_book,
color: Colors.white,
),
title: Text('English',
style: TextStyle(
color: Colors.white,
fontSize: 20,
)),
),
),
),
),
),
The non-repetitive is only the colours of linear-gradient, onTap, icon and the text. Any idea how? I tried using the void function but was unable to achieve a usable function. Hope to get some insights from you guys.
Make it a StatelessWidget class
class MyButton extends StatelessWidget {
final VoidCallback onTap;
final List<Color> colors;
final Widget icon;
final String text;
const MyButton({
Key? key,
required this.onTap,
required this.colors,
required this.icon,
required this.text,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Material(
clipBehavior: Clip.antiAlias,
shape: const StadiumBorder(),
child: Ink(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: colors,
),
),
width: double.infinity,
height: 60,
child: InkWell(
onTap: onTap,
child: ListTile(
leading: icon,
title: Text(text,
style: TextStyle(
color: Colors.white,
fontSize: 20,
),
),
),
),
),
),
);
}
}
Now it's up to you if you want those parameters as required or you will have some defaults value in case they are null etc. (for example if you don't pass colors maybe have a cosnt list of colors to use in that case)