Custom Card Shape Flutter SDK - flutter

I have developed an app with GridView on Flutter. GridView items are Card and the default card shape is Rectangle with a radius of 4.
I know there is shape property for Card Widget and it takes ShapeBorder class but I am unable to find how to use ShapeBorder class and customize my cards in GridView.
How do I go about this?

You can use it this way
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
child: Text(
'Card with circular border',
textScaleFactor: 1.2,
),
),
Card(
shape: BeveledRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
child: Text(
'Card with Beveled border',
textScaleFactor: 1.2,
),
),
Card(
shape: StadiumBorder(
side: BorderSide(
color: Colors.black,
width: 2.0,
),
),
child: Text(
'Card with Stadium border',
textScaleFactor: 1.2,
),
),

When Card I always use RoundedRectangleBorder.
Card(
color: Colors.grey[900],
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.white70, width: 1),
borderRadius: BorderRadius.circular(10),
),
margin: EdgeInsets.all(20.0),
child: Container(
child: Column(
children: <Widget>[
ListTile(
title: Text(
'example',
style: TextStyle(fontSize: 18, color: Colors.white),
),
),
],
),
),
),

You can also customize the card theme globally with ThemeData.cardTheme:
MaterialApp(
title: 'savvy',
theme: ThemeData(
cardTheme: CardTheme(
shape: RoundedRectangleBorder(
borderRadius: const BorderRadius.all(
Radius.circular(8.0),
),
),
),
// ...

An Alternative Solution to the above
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(topLeft: Radius.circular(20), topRight: Radius.circular(20))),
color: Colors.white,
child: ...
)
You can use BorderRadius.only() to customize the corners you wish to manage.

A Better way to customise Card Radius ( Also other options ) is to set theme for Card globally. So that you can use same style for Card througout the entire app. You can use this method for many other widget also.
For Card Theme you can use ThemeData.cardTheme
MaterialApp(
title: 'MySampleApp',
theme: ThemeData(
cardTheme: CardTheme(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(16.0),
),
),
),
// ............
// ............

Related

Expand Column children inside ElevatedButton

I have this code that I want the green container expanded to the full width of the ElevatedButton to make something like a bar on the button:
Card(
elevation: 20,
shadowColor: Colors.green,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(15),
),
),
child: ElevatedButton(
style: ButtonStyle(
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(15),
),
),
),
shadowColor: MaterialStateProperty.all(Colors.green),
backgroundColor: MaterialStateProperty.all(Colors.red),
),
onPressed: () {
Navigator.push(
c,
MaterialPageRoute(builder: (c) => route),
);
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ClipRRect(
borderRadius: BorderRadius.only(topRight: Radius.circular(15), topLeft: Radius.circular(15)),
child: Container(
height: 45,
color: Color.fromARGB(255, 0, 176, 44),
child: Image.asset(
"assets/icons/${icon}",
),
),
),
Text(
label,
style: const TextStyle(color: Colors.black),
),
],
),
),
)
But what I'm getting is this instead:
,
I already tried Expanded, CrossAxisAligment.Stretch on the column, but it feels like the Button itself add some constraints to its children!
It is coming from ButtonStyle of default padding, set it 0, EdgeInsets.zero.
ElevatedButton(
style: ButtonStyle(
padding:
MaterialStateProperty.all<EdgeInsets>(EdgeInsets.all(0)),
shape:
More about ButtonStyle

Flutter rounded appbar with color line

i have a rounded Appbar with a color line. Here is a Screenshot.
I want the color line to follow the rounding. Is this possible, since I have not found anything about it?
This is my code so far:
class LoginPage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"Test",
style: TextStyle(fontStyle: FontStyle.italic),
),
centerTitle: true,
elevation: 10,
backgroundColor: Colors.cyan[900],
bottom: PreferredSize(
preferredSize: Size.fromHeight(4.0),
child: SizedBox(
height: 5,
child: Container(
color: Colors.orange,
),
)),
shadowColor: Colors.cyan[900],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(30),
),
),
),
);
}
}
you can create your own custom Appbar by implementing PreferredSizeWidget
and the curved border can be achieved through nesting two containers where the space between them (padding in this case represents the stroke width) and the color of the parent container represents the color of the border.
here is full example
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {
const CustomAppBar({Key key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
height: 80.0,
padding: EdgeInsets.only(bottom: 6.0),
decoration: BoxDecoration(
color: Colors.amber,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(16.0),
bottomRight: Radius.circular(16.0),
),
),
child: Container(
height: double.infinity,
padding: EdgeInsets.symmetric(horizontal: 8.0),
decoration: BoxDecoration(
color: AppTheme.primaryColor,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(16.0),
bottomRight: Radius.circular(16.0),
),
boxShadow: [BoxShadow(color: AppTheme.primaryColor.withOpacity(.4), spreadRadius: 2.0, blurRadius: 12.0)]),
child: Center(
child: Text("Hello Custom Appbar"),
),
),
);
}
#override
Size get preferredSize => Size(double.infinity, 150.0);
}
then use it like
Scaffold(
appBar: CustomAppBar() ,
border will cover whole appbar, is this what you want?
Scaffold(
extendBody: true,
appBar: AppBar(
title: Text(
"Test",
style: TextStyle(fontStyle: FontStyle.italic),
),
centerTitle: true,
elevation: 10,
backgroundColor: Colors.cyan[900],
shadowColor: Colors.cyan[900],
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
bottom: Radius.circular(30),
),
side: BorderSide(width: 3.0, color: Colors.orange),
),
),

how can I change the background color of the header using an alertDialog in flutter?

currently only the text is only painted with the following code
AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
title: Text(
'Error',
style: TextStyle(backgroundColor: STYLES.styles["success"]),
)
.
.
.
I would like to paint the header of the alertDialog in one color, how can I do it?
You can use Container as the title.
There's a titlePadding property for AlertDialog. If title isn't null, the default value is 24. therefore you need to explicitly supply it with 0.
AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
title: Container(
color: Colors.red,
child: Text('Error'),
),
titlePadding: const EdgeInsets.all(0),
)
You can wrap the title inside a Container and align it with center.
Try something like this..
AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0)),
title: Container(
color: Colors.orange,
child: Center(
child: Text("Error"),
)),
titlePadding: const EdgeInsets.all(0),
)
Try with:
AlertDialog(
title: Container(
padding: EdgeInsets.all(15),
decoration: const BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0),
),
),
child: const Text(
"Error",
style: TextStyle(
color: Colors.white,
),
),
),
titlePadding: const EdgeInsets.all(0),
content: const Text("Message"),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
actions: <Widget>[
MaterialButton(
child: const Text("Accept"),
onPressed: () {},
)
],
),
barrierDismissible: false,
);
result

How to add a border color to an OutlineButton

How can I add a border color to an OutlineButton? I tried setting a highlightedBorderColor, but it is not working. How can I solve this issue?
This is my code:
OutlineButton(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
onPressed:(){},child: Padding(
padding: const EdgeInsets.all(12),
child: Text("add to cart",style: TextStyle(color: Colors.red,fontSize: 20),),
),highlightedBorderColor: Colors.red,
),
Add the following line to your code:
borderSide: BorderSide(width: 2.0, color: Colors.red),
This should change the border color of your button to red
Take a try with it:
OutlineButton(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
borderSide: BorderSide(color: Colors.pink, width: 1),
onPressed: () {},
child: Padding(
padding: const EdgeInsets.all(12),
child: Text(
"add to cart",
style: TextStyle(color: Colors.red, fontSize: 20),
),
),
)

How to set or remove a button (RaisedButton) corner radius?

None of the Flutter buttons seem to provide a way to change the corner radius of the button rectangle. There's a shape property but I have no idea how to use it.
You can change the radius from the shape property, and give it a RoundedRectangleBorder, here you can play with the border radius.
You will get the rounded corner only in topRight,topLeft and bottomRight in the below example:
RaisedButton(
onPressed: () {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topRight: Radius.circular(25.0),
topLeft: Radius.circular(25.0),
bottomRight: Radius.circular(25.0),
),
),
),
Since the left-sided buttons are deprecated use the recommended ones.
Deprecated vs Recommended
RaisedButton - ElevatedButton
OutlineButton - OutlinedButton
FlatButton - TextButton
ElevatedButton
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.white,
shadowColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topRight: Radius.circular(0.0),
topLeft: Radius.circular(0.0),
),
),
),
),
OutlinedButton
OutlinedButton(
style: OutlinedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(18.0),
),
side: BorderSide(width: 2, color: Colors.green),
),
onPressed: () {},
child: Text('Button'),
)
TextButton
TextButton(
child: Padding(
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
child: Text('Text here',
style: TextStyle(
color: Colors.teal,
fontSize: 14,
fontWeight: FontWeight.w500)),
),
style: TextButton.styleFrom(
primary: Colors.teal,
onSurface: Colors.yellow,
side: BorderSide(color: Colors.teal, width: 2),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(25))),
),
onPressed: () {
print('Pressed');
},
),