Border container and card - flutter

I've the following code:
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
child: InkWell(
onTap: () {
....
},
child: Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(
color: Theme.of(context).primaryColor,
width: 3.0,
)),
//borderRadius: BorderRadius.only(topLeft: const Radius.circular(5.0), topRight: const Radius.circular(5.0)),
),
height: 133,
child: ...,
),
));
}
The problem is that I am trying to set a color on the top border and assign a border radius of 5. As far as I am aware, I can't assign a color to the top border in the card. You have to set it all the way around. But you can do this on the container. The problem is that adding a radius to the Container makes the content inside it disappear.

That's an interesting mechanic... Though not ideal, I suppose you could wrap your child with a Column and make your own custom border element, so it doesn't clash with your content.
Something like this:
class MyTopBorder extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: const Radius.circular(5.0),
topRight: const Radius.circular(5.0),
),
color: Theme.of(context).primaryColor,
),
height: 3.0,
);
}
}
and then add it to your Card:
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
child: InkWell(
onTap: () {
// ...
},
child: Column(
children: <Widget>[
MyTopBorder(),
Container(
height: 133,
child: Text("..."),
),
],
),
),
),

Related

Verify if the file exist in app repository, and do conditional function

I used an image picker that allows me to retrieve and save an image in the application folder. I'm trying to display conditionally the background of my container either in black if the image doesn't exist, or the image if it does exist
I am currently trying to check with
child: (File(widget.id + '.jpg').exists())
But this is impossible because value always pays true
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
pickImage();
},
child: (File(widget.id + '.jpg').exists())
? Container(
decoration: BoxDecoration(
image: DecorationImage(
image: FileImage(File(widget.id + ".jpg")),
fit: BoxFit.cover,
),
color: Colors.black87,
borderRadius: BorderRadius.circular(5)),
child: Align(
alignment: FractionalOffset.bottomLeft,
child: MaterialButton(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(5),
bottomRight: Radius.circular(5),
),
),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
minWidth: double.infinity,
color: Colors.yellow,
onPressed: () {},
child: Text('SNAPPER')),
))
: Container(
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.circular(5)),
child: Align(
alignment: FractionalOffset.bottomLeft,
child: MaterialButton(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(5),
bottomRight: Radius.circular(5),
),
),
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
minWidth: double.infinity,
color: Colors.yellow,
onPressed: () {},
child: Text('SNAPPER')),
)));
}
}
I would also like to shorten the condition? I'm calling two containers when I could use only one and condition only the background
Thanks a lot

Container with rounded border but top border has different width than other sides

I'm trying to create a container that has a rounded border but the top border is wider than the rest to fit some text (probably using a Stack).
Something like this:
How ever I haven't found a way to achieve this. I tried this:
return Container(
decoration: BoxDecoration(
border: Border(
left: BorderSide(color: MyColors.backgroundColor1(context), width: 5, style: BorderStyle.solid),
right: BorderSide(color: MyColors.backgroundColor1(context), width: 5, style: BorderStyle.solid),
bottom: BorderSide(color: MyColors.backgroundColor1(context), width: 5, style: BorderStyle.solid),
top: BorderSide(color: MyColors.backgroundColor1(context), width: 25, style: BorderStyle.solid),
),
),
child: ...
);
But this way I can't add any border radius.
Any idea how to achieve this in Flutter?
Here is a minimum reproducible sample:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10), topRight: Radius.circular(10))),
clipBehavior: Clip.antiAlias,
child: Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(color: Colors.black, width: 20),
left: BorderSide(color: Colors.black, width: 2),
bottom: BorderSide(color: Colors.black, width: 2),
right: BorderSide(color: Colors.black, width: 2),
)),
),
),
),
);
}
}
You could also achieve this with a stack, though I suspect this is closer to what you're looking for. The key is the clip in the parent container. That will hide everything that overflows from that parent container.
EDIT
After taking a closer look at your picture, I think a stack is the best way to go. That will easily allow you to put the text up in that top border.
Here's an example widget:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Material(
child: Padding(
padding: EdgeInsets.all(20),
child: Stack(
children: [
Positioned.fill(
child: Container(
decoration: BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10))),
)),
Positioned.fill(
child: Column(children: [
Text("Sample Text",
style: TextStyle(color: Colors.white, fontSize: 18)),
Expanded(
child: Padding(
padding: const EdgeInsets.all(2.0),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10))),
),
))
]))
],
),
),
),
);
}
}
Basically, you stack a column on top of a container that is filled with your "border" color. The first element in that column is the text you want to show in the "border". Then, you fill the rest of the column with an expanded widget and container whose color is your background color. That container is wrapped in padding, which provides you with the border width on the left, right, and bottom of the widget.

Adding a border to 3/4 sides in flutter

I'm trying to add a border to 3/4 sides on a custom button (the bottom has no border). However, the code produces no border anywhere. If I give all the sides a border, it works, but does not work if I do 3/4 sides. Any help is greatly appreciated. This is the code:
class CustomButton extends StatelessWidget{
CustomButton(this.img, this.title, this.connectivity, this.link);
final img;
final title;
final connectivity;
final link;
#override
Widget build(BuildContext context){
return GestureDetector(
onTap: (){
Navigator.of(context).push(MaterialPageRoute(builder: (context) => UrlViewers(link)));
print("Link: $link");
},
child: Column(
children: [
Container(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 2),
height: 200,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
border: Border(
left: BorderSide(color: Colors.black, width: 1.0),
top: BorderSide(color: Colors.black, width: 1.0),
right: BorderSide(color: Colors.black, width: 1.0),
),
image: DecorationImage(image: NetworkImage(img), fit: BoxFit.cover)
),
),
Container(
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
color: Colors.white24,
borderRadius: BorderRadius.circular(10),
),
child: Text(title, style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold,)),
)
],
),
);
}
}
I have had the same issue earlier, apparently there is no simple way to add borders to a container while controlling its radius..
if you just remove the borderRadius parameter from the BoxDecoration it should work just fine.

Left shape inside a card

I am trying to do this card. But I don't know how to the green shape part. Any suggestions?
Inside a row create a container and give it DecorationBox and some radius to upper and bottom left corners and a second child as text. You can modify the following example code according to your needs.
Container(
child: Row(
children: [
Container(
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(5),
bottomLeft: Radius.circular(5))),
height: 20,
width: 20,
),
Text('data'),
],
)),
Exactly how I want it.
class NotificationBanner extends StatelessWidget {
final Radius radius = Radius.circular(10.0);
#override
Widget build(BuildContext context) {
return Container(
child: Row(
children: [
Container(
height: 100,
width: 8,
decoration: BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.only(
topLeft: radius,
bottomLeft: radius,
),
),
),
Expanded(
child: Container(
height: 100,
decoration: BoxDecoration(
color: kLightBlack,
borderRadius: BorderRadius.only(
topRight: radius,
bottomRight: radius,
),
),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
'Some pretty message',
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20.0),
),
),
)),
),
],
),
);
}
}

Best way to add a shadow and colored border to ClipRRect using flutter

I have a simple ClipRRect widget as the below one:
#override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(10),
child: GridTile(
child: Image.network(
imageUrl,
fit: BoxFit.cover,
),
footer: GridTileBar(
backgroundColor: Colors.black87,
leading: IconButton(
icon: Icon(Icons.favorite),
color: Theme.of(context).accentColor,
onPressed: () {},
),
title: Text(
title,
textAlign: TextAlign.center,
),
trailing: IconButton(
icon: Icon(Icons.shopping_cart),
onPressed: () {},
color: Theme.of(context).accentColor,
),
),
),
);
}
I tried to add a shadow by wrapping this widget with another Card widget to add a shadow with an elevation as the below code:
#override
Widget build(BuildContext context) {
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
elevation: 5,
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: GridTile(
child: Image.network(
imageUrl,
fit: BoxFit.cover,
),
footer: GridTileBar(
backgroundColor: Colors.black87,
leading: IconButton(
icon: Icon(Icons.favorite),
color: Theme.of(context).accentColor,
onPressed: () {},
),
title: Text(
title,
textAlign: TextAlign.center,
),
trailing: IconButton(
icon: Icon(Icons.shopping_cart),
onPressed: () {},
color: Theme.of(context).accentColor,
),
),
),
),
);
}
but I think it's not logical at all because it shows the grid item a bit smaller, and also I need add a border in this grid..
I hope this would be clear enough..
you can create your widget to take in a shadow and border colors as follows
since ClipRRect cant take in shadow or border color we use a container
clipRRect constructor
ClipRRect({Key key, BorderRadius borderRadius: BorderRadius.zero, CustomClipper<RRect> clipper, Clip clipBehavior: Clip.antiAlias, Widget child})
.
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 200,
width: 200,
child: Card(
elevation: 4.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
bottomRight: Radius.circular(10),
topRight: Radius.circular(10)),
side: BorderSide(width: 2, color: Colors.green)),
child: Center(
child: Icon(
Icons.movie,
size: 150.0,
),
),
),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.white54,
blurRadius: 5.0,
offset: Offset(0, 10),
spreadRadius: 0.5,
),
],
borderRadius: BorderRadius.circular(12),
),
);
}
}
Here's another solution which worked for me.
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: SizedBox(
child: ImageWidget(imageUrl),
width: 100,
height: 100,
),
),
width: 100,
height: 100,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.15),
blurRadius: 8,
spreadRadius: 6,
offset: const Offset(0, 0),
),
],
),
)
I implemented it like below
Stack(alignment: Alignment.center, children: [
Container(
width: DeviceUtils.getScaledHeight(context, 0.072),
height: DeviceUtils.getScaledHeight(context, 0.072),
decoration: BoxDecoration(
border: Border.all(color: Colors.green),
borderRadius: BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Colors.white54,
blurRadius: 5.0,
offset: Offset(0, 10),
spreadRadius: 0.5,
),
],
),
),
ClipRRect(
borderRadius: BorderRadius.circular(8),
child: ImageFromURL(
url: "https://i.pinimg.com/originals/63/81/a7/6381a77565b51a541d0d85cb145e7d94.jpg",
width: DeviceUtils.getScaledHeight(context, 0.07),
height: DeviceUtils.getScaledHeight(context, 0.07)),
),
])