Flutter: Material with only partial shadow - flutter

Consider the following fragment:
Material(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
),
elevation: 4.0,
child: Container(
padding: const EdgeInsets.fromLTRB(24, 30, 24, 8),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
),
),
child: Text('foo'),
)
)
This will render a drop shadow around the widget. However, I only want the shadow left/top/right, but not bottom. How can this be achieved?

You can remove Material widget and use the BoxShadow property of Container:
Container(
child: Center(
child: Container(
padding: const EdgeInsets.fromLTRB(24, 30, 24, 8),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.black12,
offset: Offset(0, -2),
spreadRadius: 1.5,
blurRadius: 2
)
],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
),
),
child: Text('foo'),
),
),
),
boxShadow can receive an array of BoxShadows. So you can have the shadow however you want:
Container(
padding: const EdgeInsets.fromLTRB(24, 30, 24, 8),
decoration: BoxDecoration(
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.black12,
offset: Offset(0, -2),
blurRadius: 2
),
BoxShadow(
color: Colors.black12,
offset: Offset(-2,-1),
blurRadius: 2
),
BoxShadow(
color: Colors.black12,
offset: Offset(2, -1),
blurRadius: 2
),
],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
),
),
child: Text('foo'),
),

Related

When i use BoxShadow Colors.grey.withOpacity(0.5) with opacity it show error

import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Welcome'),
),
body: Center(
child: Container(
alignment: Alignment.topCenter,
height: 260,
width: 260,
decoration: const BoxDecoration(
color: Colors.blue,
//borderRadius: BorderRadius.all(Radius.circular(20)),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(30),
bottomLeft: Radius.elliptical(100, 50),
bottomRight: Radius.elliptical(70, 150),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
blurRadius: 10,
spreadRadius: 5,
offset: Offset(.5, 0),
blurStyle: BlurStyle.outer,
),
],
),
padding: const EdgeInsets.all(10.0),
margin: const EdgeInsets.only(left: 10),
child: const Text(
'Hello World!',
//textAlign: TextAlign.center,
style: TextStyle(
fontSize: 50,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
),
));
}
Try this, Need to remove const
Center(
child: Container(
alignment: Alignment.topCenter,
height: 260,
width: 260,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(30),
bottomLeft: Radius.elliptical(100, 50),
bottomRight: Radius.elliptical(70, 150),
),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
blurRadius: 10,
spreadRadius: 5,
offset: Offset(.5, 0),
blurStyle: BlurStyle.outer,
),
]),
padding: const EdgeInsets.all(10.0),
margin: const EdgeInsets.only(left: 10),
child: const Text(
'Hello World!',
//textAlign: TextAlign.center,
style: TextStyle(
fontSize: 50,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
),
Please remove "const" before BoxDecoration widget. And then you could assign withOpacity for Color in the BoxShadow.
Before
After

How do I get rid of this flickering box around my card?

When I scroll the box flickers white.
Here's a video of it:
https://flutter-project.tumblr.com/post/656161576589672448
Here's my code:
body: ListView(
children: <Widget>[
Container(
height: 120,
color: Colors.white
),
Card(
elevation: 0,
color: Colors.white,
margin: EdgeInsets.zero,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0),),
child: Container(
height: 20,
decoration: BoxDecoration(
color: Color(0xFF1F305E),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),),
),
),
),
The look I'm trying the achieve:
Here's a video of it:
https://flutter-project.tumblr.com/post/656241232200220672
Not sure if this is the best way but adding a box shadow got rid of it...
Card(
elevation: 0,
color: Colors.white,
margin: EdgeInsets.zero,
shadowColor: Color(0xFF1F305E),
borderOnForeground: false,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0),),
child: Container(
height: 20,
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
BoxShadow(
spreadRadius: 5,
color: Color(0xFF1F305E),
offset: Offset(1,1,)
)
],
color: Color(0xFF1F305E),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30.0),
bottomRight: Radius.zero,
bottomLeft: Radius.zero),
),
),
),

How to draw rounded rectangle with borders only on top?

There are multiple ways to draw a rounded rectangle. I want to draw a rounded rectangle with content inside. However, only the top of the rectangle should be rounded.
I tried
Container(
decoration:
BoxDecoration(border: Border(top: BorderSide(color: Colors.red))),
child: Column(
children: [Text("hello")],
));
but I get a red line with "hello" on bottom. Makes no sense.
With BorderRadius.vertical you can choose top corners or bottom corners.
decoration: BoxDecoration(
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),
),
),
And with BorderRadius.only you can choose any corner.
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
It's a little trick
Container(
height: 100,
width: 150,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.all(
const Radius.circular(15.0),
),
),
child: Container(
margin: const EdgeInsetsDirectional.only(top: 2),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.only(
topLeft: const Radius.circular(13.0),
topRight: const Radius.circular(13.0),
),
),
child: Column(
children: [
Text("hello"),
],
)
),
)
Try this
Container(
decoration: ShapeDecoration(
color: Colors.yourColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(16.0),
topRight: Radius.circular(16.0),
),
),
),
),

How to display a text at the bottom left of the Image

I want to display a text at the bottom left if the image to be like as the below figure:
and this the below code is related:
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/city.dart';
class ProvinceItem extends StatelessWidget {
#override
Widget build(BuildContext context) {
final city = Provider.of<City>(context, listen: false);
// final cart = Provider.of<Cart>(context, listen: false);
return Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
splashColor: Colors.transparent,
onTap: () => {},
child: Container(
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(16.0)),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.grey.withOpacity(0.6),
offset: const Offset(2.5, 2.5),
blurRadius: 16,
),
],
),
margin: EdgeInsets.all(2),
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15),
topRight: Radius.circular(15),
bottomLeft: Radius.circular(15),
bottomRight: Radius.circular(15),
),
child: Image.asset(
city.cityImage,
fit: BoxFit.cover,
),
),
),
),
);
}
}
I have just create a list of Grid widget and as every list of grid item have a grid item just shown like the previous code...
as I need the best suggestion for this Grid item as it only contains just an image and a text with some of shadows..
Stack widget will do the trick for you.
Stack(
children: <Widget>[
InkWell(
splashColor: Colors.transparent,
onTap: () => {},
child: Container(
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(16.0)),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.grey.withOpacity(0.6),
offset: const Offset(2.5, 2.5),
blurRadius: 16,
),
],
),
margin: EdgeInsets.all(2),
child: ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15),
topRight: Radius.circular(15),
bottomLeft: Radius.circular(15),
bottomRight: Radius.circular(15),
),
child: Image.asset(
city.cityImage,
fit: BoxFit.cover,
),
),
),
),
Container(
margin: EdgeInsets.only(left: 20, bottom: 20),
child: Align(
alignment: Alignment.bottomLeft,
child: Text(
'Text',,
style: TextStyle(color: Colors.white, fontSize: 20),
)),
),
],
),

How to make one side circular border of widget with flutter?

I'm trying to build one side circular border with Container widget in flutter.
I have searched for it but can't get any solution.
Container(
width: 150.0,
padding: const EdgeInsets.all(20.0),
decoration: BoxDecoration(
// borderRadius: BorderRadius.circular(30.0),
/* border: Border(
left: BorderSide()
),*/
color: Colors.white
),
child: Text("hello"),
),
Use BorderRadius.only and provide the sides
return Center(
child: Container(
height: 100,
width: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topRight: Radius.circular(40),
),
border: Border.all(
width: 3,
color: Colors.green,
style: BorderStyle.solid,
),
),
child: Center(
child: Text(
"Hello",
),
),
),
);
Output
You can achieve this by following code for creating your widget :
return Container(
width: 150.0,
padding: const EdgeInsets.all(20.0),
decoration: BoxDecoration(
shape: BoxShape.rectangle,
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.0),
topRight: Radius.zero,
bottomLeft: Radius.zero,
bottomRight: Radius.zero,
),
),
child: Text(
"hello",
),
);
This way you can have your top left sided circular border with Container widget in flutter.
Another way of doing this is to use the ClipRRect widget.
Simply wrap your widget with ClipRRect and give a radius.
You can specify which corner you want to make round.
ClipRRect(
borderRadius: BorderRadius.only(topRight: Radius.circular(10)),
child: Container(
height: 40,
width: 40,
color: Colors.amber,
child: Text('Hello World!'),
),
);
If you want one side of a container rounded you want to use BorderRadius.only and specify which corners to round like this:
Container(
width: 150.0,
padding: const EdgeInsets.all(20.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topRight: Radius.circular(40.0),
bottomRight: Radius.circular(40.0)),
color: Colors.white),
child: Text("hello"),
),
Also You can do it with 'Shape Function'.
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topRight: Radius.circular(15.0),
topLeft: Radius.circular(15.0),
),
also do can do as follows,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(30.0),
bottomLeft: const Radius.circular(30.0),
),
You can also set the radius of each side.
margin: EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
fromLTRB= from Left, Top, Right, Bottom