Flutter- How to use image in container? - flutter

I am building an app and I am using a Container to design a card but here is a thing image should be 1/3 of the container and not fill up the entire container.
Here is the code for the container:
Container(
height: 240,
width: 160,
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage('images/rest.jpg'),fit: BoxFit.cover),
borderRadius: BorderRadius.all(Radius.circular(10)),
color: Color(0xFFFDEEDD),
),
child: Column(
children: <Widget>[
Text('Hello'),
Row(
children: <Widget>[
Text('World'),
Text('4.2'),
],
)
],
),
),
Card I am trying to replicate

I think it would be better to use a ClipRRect here instead. If you use material, you can also use ListTile :
return Material(
child: Center(
child: Container(
color: Colors.black12,
width: 250,
child: Column(
mainAxisSize: MainAxisSize.min,
children:[
ClipRRect(
child:Image.network("https://via.placeholder.com/250"),
borderRadius: BorderRadius.all(Radius.circular(10)),
),
ListTile(
title: Text("The fifties"),
subtitle: Text("Subtitle"),
)
]
)
)
)
);

Related

Flutter Image in Card Widget does not fill the whole card

I want to have a glass look background for my cards. When I pass the container in the card widget, the image is in the middle and too small.
Example:
Example of how it looks
My build method:
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height;
return Card(
elevation: 6,
color: backgroundColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(14),
),
child: InkWell(
onTap: () {},
child: Container(
width: width / 1.6,
height: height / 4,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/card/test.png'),
fit: BoxFit.fitWidth,
alignment: Alignment.topCenter,
),
),
child: Padding(
padding: const EdgeInsets.only(left: 16.0, right: 16.0, top: 15, bottom: 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(groupData.name, overflow: TextOverflow.ellipsis,),
Text(groupData.moduleId, overflow: TextOverflow.ellipsis,),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(child: Text(moduleData.name, overflow: TextOverflow.ellipsis,)),
Image.asset('assets/images/card/group_card_icon.png', height: 60, width: 60, fit: BoxFit.cover,)
],
)
],
),
),
),
),
);
Maybe it's not a code problem and your test.png image has empty transparent space surrounding the actual glass-like background, i just copy pasted your code but changed the fit to BoxFit.fill in your DecorationImage and it works well:
When you want to fill the image in a complete area you need to use fill, replace this with your decoration part of the decoration.
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/card/test.png'),
fit: BoxFit.fill,
alignment: Alignment.topCenter,
),
),

Overlap widget inside card widget in Flutter

I wanted to place two widgets (a text and a picture) inside a card, however the picture should go over the card as the image shows:
But I don't know how to make the picture overlapping the card.
This is my code:
Card(
margin: EdgeInsets.zero,
color: Theme.of(context).colorScheme.primary,
clipBehavior: Clip.antiAlias,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: Row(
children: [
Container(
alignment: Alignment.bottomLeft,
margin: const EdgeInsets.all(12),
height: 70,
child: Text(
"This is a text widget.",
style: Theme.of(context).textTheme.bodyText1,
),
),
Image.asset(
'images/picture1.png',
scale: 5,
),
],
),
),
You can use stack widget along with positioned widget to achieve the UI
Stack
Card
Container
Card(
margin: EdgeInsets.zero,
color: Theme.of(context).colorScheme.primary,
clipBehavior: Clip.antiAlias,
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: Stack(
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Container(
alignment: Alignment.bottomLeft,
margin: const EdgeInsets.all(12),
height: 70,
child: Text(
"This is a text widget.",
style: Theme.of(context).textTheme.bodyText1,
),
),
Image.asset(
'images/picture1.png',
scale: 5,
),
],
),
],
),
),
you can use stack under card widget

How can I use ClipRRect and ListTile inside Row?

I want to use ClipRRect and ListTile inside Row but it gives an error. Please help me to resolve this error.
Code
body: Column(
children: [
SizedBox(height: 20),
Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(20))),
child: Row(
children: [
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image(image: AssetImage('mobile1.jpg'), ),
), ),
ListTile(
title: Text("I Phone 6"),
subtitle: Text("data"),
)
])),
]
), );
Solution:
This is an overflow problem.
It can be solved like this, (has some extras 😉)
return Scaffold(
body: Column(
children: <Widget>[
SizedBox(height: 20),
Card(
// this elevation is purely for shape and is not related to
// solving the problem.
elevation: 10,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
// this padding widget is purely for shape and is not related to
// solving the problem.
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Container(
// here I added some height and width so the image
// follows them and be smaller or larger than the card.
width: 130,
height: 130,
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
// your image, I used a network one.
//
// child: Image(
// image: AssetImage('mobile1.jpg'),
// ),
child: Image.network(
"https://images.pexels.com/photos/699122/pexels-photo-699122.jpeg",
// this line makes the image cover the area, this will
// allow the roundness to appear.
fit: BoxFit.cover,
),
),
),
// this expanded widget makes the list tile take the empty
// space after the image and not any less or more.
Expanded(
child: ListTile(
title: Text("I Phone 6"),
subtitle: Text("data"),
),
),
],
),
),
),
],
),
);
Result:
For the exact solving you need to give the stacktrace. Thought it was a layout overflow issue and I solved as follows:
Column(children: [
SizedBox(height: 20),
Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20))),
child: Row(
children: [
Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(20),
child: Image(
image: AssetImage('mobile1.jpg'),
),
),
),
Expanded(
child: ListTile(
title: Text("I Phone 6"),
subtitle: Text("data"),
),
)
])),
])

Flutter container write text over background image

I want to make a design like following
I build the base layout with background image. Following is the code to achieve that. Now i want to put "Grocery store" Text on top of this image and other widgets. How can i do that ?
Widget build(BuildContext context) {
return Container(
height: 190.0,
width: size.width,
margin: const EdgeInsets.symmetric(
horizontal: 16.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
width: size.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(11.0),
image: DecorationImage(
image: NetworkImage(https://www.exampledomain.com/images/background.jpg),
fit: BoxFit.cover,
),
),
// child: ????
),
),
],
),
),
],
),
);}
child: Container(
width: size.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(11.0),
image: DecorationImage(
image: NetworkImage(https://www.exampledomain.com/images/background.jpg),
fit: BoxFit.cover,
),
),
child: Text('Grocery store'),
//padding: <-- Using to shift text position a little bit for your requirement
),
Stack/Align is your friend.
Take a look here https://api.flutter.dev/flutter/widgets/Stack-class.html
Here is a basic example (based in your code):
Widget build(BuildContext context) {
return Stack(
children: [
Align(
alignment: Alignment.center,
child: Container(
alignment: Alignment.center,
color: Colors.red,
height: 190.0,
width: size.width,
margin: const EdgeInsets.symmetric(
horizontal: 16.0,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
child: Row(
children: <Widget>[
Expanded(
child: Container(
width: size.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(11.0),
image: DecorationImage(
image: NetworkImage(
"http://www.exampledomain.com/images/background.jpg"),
//fit: BoxFit.cover,
),
),
),
),
],
),
),
],
),
),
),
Align(
alignment: Alignment.center,
child: Text("Grocery store"))
]
);
}

Adding widgets upon image -flutter, dart

I have an image like this
and i need to add widgets upon this image. It should look like the contents are just on the mobile screen. I tried searching for a way but i dint get any. I tried using stack like this:
Container(
child: Stack(
children: <Widget>[
Positioned(
child: Container(
padding: EdgeInsets.fromLTRB(60, 110, 60, 90),
//body
),
),
Container(width: 1000,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('Assets/Images/transparentMobile.png'),
fit: BoxFit.fill)),
),
],
),
),
It worked correctly on my phone:
but on other phone it looks very different. How do i write a single code for multiple screens?
Finally i got a way to do it. Instead of working hard on the code, i just cropped the image into 3 parts :
top :
body:
bottom:
and added them to columns like this:
AspectRatio(
aspectRatio: 1/2,
child: Container(
padding: EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('Assets/Images/Mobile/mobileTop.png'),
fit: BoxFit.fill
)
),
),
),
Expanded(
flex: 7,
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('Assets/Images/Mobile/mobileBody.png',),
fit: BoxFit.fill
)
),
height: 100,
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
automaticallyImplyLeading: false,
title: Text('My Application'),
),
body: Center(
child: Text(
'Hello World!',
style: TextStyle(color: Colors.black),
),
),
backgroundColor: Colors.white,
),
),
),
Expanded(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('Assets/Images/Mobile/mobileBottom.png'),
fit: BoxFit.fill
)
),
),
),
],
),
),
),
and the image now looks much similar on most of the devices now:
Thanks for your help!