How to reduce repetitive code for buttons in flutter? - 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)

Related

IconButton's color displays weirdly

I had tried to imitate to this below tips to display the IconButton the same as below image:
These are links I had made reference to:
How to set background color for an icon button?
https://dartpad.dev/?null_safety=true&id=6182feb015bbb179e08bf5eb61cbabac
This is my code:
import 'package:cached_network_image/cached_network_image.dart';
import 'package:fakebook_frontend/screens/home/widgets/ProfileAvatar.dart';
import 'package:flutter/material.dart';
import 'package:fakebook_frontend/models/Models.dart';
class OnlineUsers extends StatelessWidget {
final List<User> onlineUsers;
const OnlineUsers({Key? key, required this.onlineUsers}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
height: 80, // mong muốn không fix cứng
color: Colors.white,
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 4),
child: Material(
color: Colors.transparent,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: onlineUsers.length,
itemBuilder: (BuildContext context, int index) {
if(index == 0) {
return Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Stack(
alignment: Alignment(0,-1),
children: [
CircleAvatar(
radius: 22.0,
backgroundImage: CachedNetworkImageProvider(onlineUsers[0].imageUrl),
),
Positioned(
right: 1.0,
bottom: 0.0,
child: Ink(
decoration: ShapeDecoration(
color: Colors.blue,
shape: CircleBorder(),
),
child: IconButton(
padding: EdgeInsets.zero,
constraints: BoxConstraints(),
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
icon: Icon(Icons.add_circle),
iconSize: 16,
color: Colors.white,
onPressed: () {
print("hello");
},
),
),
),
],
),
Text('Tin của bạn', style: TextStyle(fontSize: 10, fontWeight: FontWeight.normal, color: Colors.black45)),
],
);
}
return Padding(
padding: EdgeInsets.symmetric(horizontal: 6),
child: ProfileAvatar(avtUrl: onlineUsers[index].imageUrl, name: onlineUsers[index].name, isActive: true));
}),
),
);
}
}
Please notice to the first IconButton to see which Widget is error.
Please help me to draw the same as that above image. Thank you very much
Is this what you want?
You can achieve it very easy using, following your code it should be something like this:
Positioned(
right: 1.0,
bottom: 0.0,
child: IconButton(
padding: EdgeInsets.zero,
constraints: const BoxConstraints(),
color: Colors.white,
icon: const DecoratedBox(
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.blue,
),
child: Icon(
Icons.add,
),
),
splashRadius: 8,
iconSize: 16,
onPressed: () {
print("hello");
},
),
),
The IconData you used here Icon(Icons.add_circle) has a circle around the add icon.
Try: Icon(Icons.add_rounded)

How to implement Round Shaped AppBar with Gradient Background in Flutter?

I want to achieve an AppBar which has a rounded bottom radius and gradient background. Also, I need to place some other content like cards within AppBar using the bottom property.
Here is my code:
import 'package:flutter/material.dart';
void main() => runApp(const Toddly());
class Toddly extends StatelessWidget {
const Toddly({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Toddly',
home: Scaffold(
appBar: AppBar(
leading: GestureDetector(
onTap: () {},
child: Container(
margin: const EdgeInsets.only(left: 10),
padding: const EdgeInsets.all(2),
decoration: const BoxDecoration(
color: Color.fromARGB(255, 158, 77, 130),
shape: BoxShape.circle,
),
child: const CircleAvatar(
backgroundImage: AssetImage('assets/images/profilePicture.png'),
),
),
),
title: const Text(
'Julia',
style: TextStyle(
fontFamily: 'Nunito',
),
),
centerTitle: true,
actions: [
IconButton(
onPressed: () {},
icon: const Icon(Icons.notifications_outlined),
),
IconButton(
onPressed: () {},
icon: const Icon(Icons.menu),
),
],
bottom: PreferredSize(
preferredSize: const Size.fromHeight(150.0),
child: Container(
height: 100,
width: 100,
color: Colors.white,
alignment: Alignment.center,
child: const Text('Some content'),
),
),
flexibleSpace: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0xFF72326a),
Color(0xFF321c53),
],
),
),
),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(30.0),
),
),
),
backgroundColor: const Color(0xFFfef1ee),
),
);
}
}
I used the shape property to implement rounded bottom AppBar.
I did gradient background using flexibleSpace.
And, I used bottom to add my other content under within AppBar.
However when I run all above code. rounded bottom appBar is not working.
image source
And, if I remove flexibleSpace it is showing, but I lost my gradient.
image source
Please, help me to figure it out and is it possible to do that without creating custom AppBars since I saw several approaches.
Thanks
All you just need to add corner radius to gradient container as well attaching you the updated code.
import 'package:flutter/material.dart';
void main() => runApp(const Toddly());
class Toddly extends StatelessWidget {
const Toddly({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Toddly',
home: Scaffold(
appBar: AppBar(
leading: GestureDetector(
onTap: () {},
child: Container(
margin: const EdgeInsets.only(left: 10),
padding: const EdgeInsets.all(2),
decoration: const BoxDecoration(
color: Color.fromARGB(255, 158, 77, 130),
shape: BoxShape.circle,
),
child: const CircleAvatar(
backgroundImage: AssetImage('assets/images/profilePicture.png'),
),
),
),
title: const Text(
'Julia',
style: TextStyle(
fontFamily: 'Nunito',
),
),
centerTitle: true,
actions: [
IconButton(
onPressed: () {},
icon: const Icon(Icons.notifications_outlined),
),
IconButton(
onPressed: () {},
icon: const Icon(Icons.menu),
),
],
bottom: PreferredSize(
preferredSize: const Size.fromHeight(150.0),
child: Container(
height: 100,
width: 100,
color: Colors.white,
alignment: Alignment.center,
child: const Text('Some content'),
),
),
flexibleSpace: Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(30.0),
),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0xFF72326a),
Color(0xFF321c53),
],
),
),
),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(30.0),
),
),
),
backgroundColor: const Color(0xFFfef1ee),
),
);
}
}

Get callback from custom alert dialog

I need to execute a function after calling a custom alert dialog.
Here you have how am I calling the custom Alert Dialog:
onTap: () async {
showDialog(
barrierColor: Colors.black26,
context: context,
builder: (context) {
return CustomAlertDialog(
title: "confirmarborrarusuario".tr(),
description: "borrarusuario".tr(),
imagen: widget.usuarioInterno.imagen,
email: widget.usuarioInterno.email,
);
});
},
And here you have the complete code for the Custom Dialog:
import 'package:flutter/material.dart';
class CustomAlertDialog extends StatefulWidget {
const CustomAlertDialog({
Key? key,
required this.title,
required this.description,
required this.imagen,
required this.email,
}) : super(key: key);
final String title, description, imagen, email;
#override
_CustomAlertDialogState createState() => _CustomAlertDialogState();
}
class _CustomAlertDialogState extends State<CustomAlertDialog> {
#override
Widget build(BuildContext context) {
return Dialog(
elevation: 0,
backgroundColor: Color(0xffffffff),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 15),
Text(
"${widget.title}",
style: TextStyle(
color: Colors.black38,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 15),
widget.imagen.length > 1 ? Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
radius: 55,
backgroundColor: Colors.red,
child: CircleAvatar(
radius: 50,
backgroundImage: NetworkImage(widget.imagen),
),
),
): Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
radius: 55,
backgroundColor: Colors.red,
child: CircleAvatar(
radius: 50,
backgroundImage: AssetImage('imagenes/nofoto.png'),
),
),
),
SizedBox(height: 20),
Text("${widget.email}",style: TextStyle(color: Colors.black,fontSize: 16),),
Divider(
height: 1,
),
Container(
width: MediaQuery.of(context).size.width,
height: 50,
child: InkWell(
highlightColor: Colors.grey[200],
onTap: () {
//do somethig
},
child: Center(
child: Text(
"${widget.description}",
style: TextStyle(
fontSize: 18.0,
color: Colors.red,
fontWeight: FontWeight.bold,
),
),
),
),
),
Divider(
height: 1,
),
Container(
width: MediaQuery.of(context).size.width,
height: 50,
child: InkWell(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(15.0),
bottomRight: Radius.circular(15.0),
),
highlightColor: Colors.grey[200],
onTap: () {
Navigator.of(context).pop();
},
child: Center(
child: Text(
"Cancelar",
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.normal,
),
),
),
),
),
],
),
),
);
}
}
What I need is to get a callback from the dialog telling me if the user has clicked on the first button or not, if the user clicks on the cancel button its ok, the dialog is dismissed and enough
This way u can send callback modify it according to your need.
onTap: () async {
showDialog(
barrierColor: Colors.black26,
context: context,
builder: (context) {
return CustomAlertDialog(
title: "confirmarborrarusuario".tr(),
description: "borrarusuario".tr(),
imagen: widget.usuarioInterno.imagen,
email: widget.usuarioInterno.email,
callback:(value}{
// value from call back whatever u send when u tap on widget
print(value);
}
);
});
},
Updated Code
class CustomAlertDialog extends StatefulWidget {
const CustomAlertDialog({
Key? key,
required this.title,
required this.description,
required this.imagen,
required this.email,
this.callback,
}) : super(key: key);
final String title, description, imagen, email;
final Function callback,
#override
_CustomAlertDialogState createState() => _CustomAlertDialogState();
}
class _CustomAlertDialogState extends State<CustomAlertDialog> {
#override
Widget build(BuildContext context) {
return Dialog(
elevation: 0,
backgroundColor: Color(0xffffffff),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 15),
Text(
"${widget.title}",
style: TextStyle(
color: Colors.black38,
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 15),
widget.imagen.length > 1 ? Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
radius: 55,
backgroundColor: Colors.red,
child: CircleAvatar(
radius: 50,
backgroundImage: NetworkImage(widget.imagen),
),
),
): Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
radius: 55,
backgroundColor: Colors.red,
child: CircleAvatar(
radius: 50,
backgroundImage: AssetImage('imagenes/nofoto.png'),
),
),
),
SizedBox(height: 20),
Text("${widget.email}",style: TextStyle(color: Colors.black,fontSize: 16),),
Divider(
height: 1,
),
Container(
width: MediaQuery.of(context).size.width,
height: 50,
child: InkWell(
highlightColor: Colors.grey[200],
onTap: () {
//do somethig
widget.callback.call('pass here value');
},
child: Center(
child: Text(
"${widget.description}",
style: TextStyle(
fontSize: 18.0,
color: Colors.red,
fontWeight: FontWeight.bold,
),
),
),
),
),
Divider(
height: 1,
),
Container(
width: MediaQuery.of(context).size.width,
height: 50,
child: InkWell(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(15.0),
bottomRight: Radius.circular(15.0),
),
highlightColor: Colors.grey[200],
onTap: () {
Navigator.of(context).pop();
},
child: Center(
child: Text(
"Cancelar",
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.normal,
),
),
),
),
),
],
),
),
);
}
}

onPresssed() to navigate to next page not working flutter

I use onPresssed() to navigate to next page (details_screen.dart) by clicking card 1. but its throws errors. i think I placed the code in the wrong place. can anyone help to place the code correctly? IN THE CODE I HAVE COMMENTED ON THE CODE BEACUSE ITS NOT WORKING. can anyone please help me on this
home_page.dart
import 'package:flutter/material.dart';
import 'package:fashion_app/color_filters.dart';
import 'package:fashion_app/details_screen.dart';
class HomePage extends StatefulWidget{
#override
_HomePageState createState ()=> _HomePageState();
}
class _HomePageState extends State <HomePage> {
final double _borderRadious = 24;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Fashion store'),
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
buildSearchInput(),
buildColoredCard1(),
buildColoredCard2(),
buildColoredCard3(),
],
),
);
}
Widget buildSearchInput() => Container(
decoration: BoxDecoration(
color: Colors.grey[200], borderRadius: BorderRadius.circular(20)),
child: Padding(
padding: const EdgeInsets.only(left: 10.0, right: 20),
child: Row(
children: [
Icon(
Icons.search,
size: 30,
color: Colors.grey,
),
Flexible(
child: TextField(
decoration: InputDecoration(border: InputBorder.none),
),
),
],
),
),
);
Widget buildColoredCard1() =>
Card(
shadowColor: Colors.red,
elevation: 8,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.red],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
'Summer Collection',
style: TextStyle(
fontSize: 28,
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 50),
Text(
'This card is rounded and has a gradient',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
// itemBuilder: (context, index)=>
// ItemCard(product:products[index]),
),
],
),
),
// onPressed:(){
// Navigator.push(
// context,
// MaterialPageRoute(builder: context)=> const DetailsScreen ());
);
}
Widget buildColoredCard2() =>
Card(
shadowColor: Colors.red,
elevation: 8,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.red],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Winter Collecton',
style: TextStyle(
fontSize: 28,
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 50),
Text(
'This card is rounded and has a gradient',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
],
),
),
);
Widget buildColoredCard3() =>
Card(
shadowColor: Colors.red,
elevation: 8,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.red],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Offer',
style: TextStyle(
fontSize: 30,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 50),
Text(
'This card is rounded and has a gradient',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
],
),
),
);
// class ItemCard extends StatelessWidget{
//
// final
// }
details_screen.dart
import 'package:flutter/material.dart';
class DetailsScreen extends StatelessWidget {
const DetailsScreen({required Key key,}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white70,
);
}
}
You can use GestureDetector widget for navigation, just wrap your card widget with GestureDetector widget.
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context)=> const DetailsScreen ());
);
},
child : Card(...)
)
you are putting semicolon, please compare code, it should be coma not a semicolon
onPressed:(){
Navigator.push(
context,
MaterialPageRoute(builder: context)=> const DetailsScreen
());
}
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const DetailsScreen ()),
);
}
//I check the complete code you are putting two widgets outside the HomPage class and if you want to add on click property you have to wrap your card on any gesture recogniser like GestureDetector, InkWell Please check my code its help you, Thanks
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget{
#override
_HomePageState createState ()=> _HomePageState();
}
class _HomePageState extends State <HomePage> {
final double _borderRadious = 24;
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Fashion store'),
),
body: ListView(
padding: const EdgeInsets.all(16),
children: [
buildSearchInput(),
buildColoredCard1(),
buildColoredCard2(),
buildColoredCard3(),
],
),
);
}
Widget buildSearchInput() => Container(
decoration: BoxDecoration(
color: Colors.grey[200], borderRadius: BorderRadius.circular(20)),
child: Padding(
padding: const EdgeInsets.only(left: 10.0, right: 20),
child: Row(
children: [
Icon(
Icons.search,
size: 30,
color: Colors.grey,
),
Flexible(
child: TextField(
decoration: InputDecoration(border: InputBorder.none),
),
),
],
),
),
);
Widget buildColoredCard1() =>
InkWell(
onTap: (){
Navigator.push(context, MaterialPageRoute(builder: (context)=> DetailScreen()));
},
child: Card(
shadowColor: Colors.red,
elevation: 8,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.red],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
'Summer Collection',
style: TextStyle(
fontSize: 28,
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 50),
Text(
'This card is rounded and has a gradient',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
// itemBuilder: (context, index)=>
// ItemCard(product:products[index]),
),
],
),
),
// onPressed:(){
// Navigator.push(
// context,
// MaterialPageRoute(builder: context)=> const DetailsScreen ());
),
);
Widget buildColoredCard2() =>
Card(
shadowColor: Colors.red,
elevation: 8,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.red],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Winter Collecton',
style: TextStyle(
fontSize: 28,
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 50),
Text(
'This card is rounded and has a gradient',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
],
),
),
);
Widget buildColoredCard3() =>
Card(
shadowColor: Colors.red,
elevation: 8,
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(24),
),
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.redAccent, Colors.red],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Offer',
style: TextStyle(
fontSize: 30,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 50),
Text(
'This card is rounded and has a gradient',
style: TextStyle(
fontSize: 20,
color: Colors.white,
),
),
],
),
),
);
}
class DetailScreen extends StatefulWidget {
const DetailScreen({Key? key}) : super(key: key);
#override
_DetailScreenState createState() => _DetailScreenState();
}
class _DetailScreenState extends State<DetailScreen> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white70,
);
}
}
// class ItemCard extends StatelessWidget{
//
// final
// }
You were missing a bracket
onPressed:(){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const DetailsScreen ());
}
You should use this to navigate. Do not forget the spaces, brackets and semicolons. They are so important!
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => Design1()));
},
there is ( missed
Navigator.push(context, MaterialPageRoute(builder: (context) =>
const DetailsScreen()),

How to make Elevated Button with Gradient background?

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