Flutter how to handle image with fixed size inside box? - image

I am new to Flutter and I like it but I am not comfortable building layouts.
I am working on an app that contains a ListView of Cards.
Each card is inside a Container and contains an image (with fixed height and width) and a text.
I am not able to place the image correctly inside the Card. I want the image to cover the width of box.
Thanks.
This is the code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final title = 'MyApp';
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: ListView(
children: <Widget>[
Container(
margin:EdgeInsets.all(8.0),
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: InkWell(
onTap: () => print("ciao"),
child: Column(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
child: Image.asset(
'img/britannia.jpg',
width: 300,
height: 150,
fit:BoxFit.fill
),
),
ListTile(
title: Text('Pub 1'),
subtitle: Text('Location 1'),
),
],
),
),
),
),
],
),
),
);
}
}

You need to add - crossAxisAlignment: CrossAxisAlignment.stretch, in Column so that children can take up horizontal space.
Working Code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final title = 'MyApp';
return MaterialApp(
title: title,
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: ListView(
children: <Widget>[
Container(
margin:EdgeInsets.all(8.0),
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: InkWell(
onTap: () => print("ciao"),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch, // add this
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
child: Image.network(
'https://placeimg.com/640/480/any',
// width: 300,
height: 150,
fit:BoxFit.fill
),
),
ListTile(
title: Text('Pub 1'),
subtitle: Text('Location 1'),
),
],
),
),
),
),
Container(
margin:EdgeInsets.all(8.0),
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: InkWell(
onTap: () => print("ciao"),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
child: Image.network(
'https://placeimg.com/640/480/any',
// width: 300,
height: 150,
fit:BoxFit.fill
),
),
ListTile(
title: Text('Pub 1'),
subtitle: Text('Location 1'),
),
],
),
),
),
),
Container(
margin:EdgeInsets.all(8.0),
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
child: InkWell(
onTap: () => print("ciao"),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(8.0),
topRight: Radius.circular(8.0),
),
child: Image.network(
'https://placeimg.com/640/480/any',
// width: 300,
height: 150,
fit:BoxFit.fill
),
),
ListTile(
title: Text('Pub 1'),
subtitle: Text('Location 1'),
),
],
),
),
),
),
],
),
),
);
}
}
output:

I don't know how.. But this really worked to keep image with fixed size in container
Just add Alignment in container
Container(
height: double.infinity,
alignment: Alignment.center, // This is needed
child: Image.asset(
Constants.ASSETS_IMAGES + "logo.png",
fit: BoxFit.contain,
width: 300,
),
);

This worked for me
Image.network(imageUrl, fit: BoxFit.fitWidth,),

Put Image widget inside container and give alignment center to container and specific width-height to the image.
return Container(
alignment: Alignment.center,// use aligment
color: Color.fromRGBO(0, 96, 91, 1),
child: Image.asset('assets/images/splash_logo.png',
height: 150,
width: 150,
fit: BoxFit.cover),
);

Image.asset(
'assets/images/desert.jpg',
height: 150,
width: MediaQuery.of(context).size.width,
fit:BoxFit.cover
)

child: Container(
alignment: Alignment.center,// use aligment
child: Image.asset(
'assets/images/call.png',
height: 45,
width: 45,
fit: BoxFit.cover,
),
),
Use this if you want with border
Center(
child: Container(
margin: EdgeInsets.only(top: 75),
width: 120,
height: 120,
alignment: Alignment.center,
child: Image.asset(
"assets/images/call.png",
fit: BoxFit.cover,
height: 45,
width: 45,
),
decoration: new BoxDecoration(
color: Colors.white,
borderRadius: new BorderRadius.all(new Radius.circular(120)),
border: new Border.all(
color: Colors.blue,
width: 4.0,
),
),
),
)

I used this and worked fine with me!
Container(
width: MediaQuery.of(context).size.width,
height: 175,
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(YOUTUBE_THUMBNAIL_PART_ONE +
video.key +
YOUTUBE_THUMBNAIL_PART_TWO),
),
)),

What you want to do is probably use the size of the bigger container.
In that case your media should occupy the whole dedicate space:
return Container(
alignment: Alignment.center,
height: double.infinity,
width: double.infinity,
child: Image.asset(
'', //TODO fill path
height: double.infinity,
width: double.infinity,
fit: BoxFit.cover,
),
);
You can play with the fit value:
BoxFit.cover will cover the whole container
BoxFit.fitWidth will fit the width and eventually put horizontal white bars to fill the space
BoxFit.fitHeight will fit the height and eventually put vertical white bars to fill the space

If doable with the fit property, I let this very clear cheat sheet (chapter fit Property) detail everything: https://medium.com/jlouage/flutter-boxdecoration-cheat-sheet-72cedaa1ba20

All you need is fit property of Image class:
Image.asset(
'your_image_asset',
fit: BoxFit.fill, // Expands to fill parent (changes aspect ratio)
)
or
Image.asset(
'your_image_asset',
fit: BoxFit.cover, // Zooms the image (maintains aspect ratio)
)

I had the same problem, I just added in the Image.asset class the fit:BoxFit.fill after using the MediaQuery class to get the width of the screen**
Container(
child:Image.asset(link,fit: BoxFit.fill,)
width: screensize.width,
height: 150,
))

Related

How to position an image at the top in a stack?

So I am trying to have an image at the top followed by a list below it. Intially, the list is contracted. When the list expands, I want it to overlap the image.
Initial Position - here I want the image to be at the top
Final Position - this is correct
The Problem is - If I position the image to the top, the list also moves to the top (in the initial position) which is not what I want. Also, if I use a column to position the image at the top (and the list below it) then the list does not expand all the way up to the top; it stays (and expands) below the image.
#override
Widget build(BuildContext context) {
double maxHeight = MediaQuery.of(context).size.height;
return Scaffold(
//resizeToAvoidBottomInset: false,
//backgroundColor: Color(0xFFd8e3e3),
body: Align(
child: SingleChildScrollView(
//to avoid bottom overflow error
child: Padding(
padding: const EdgeInsets.fromLTRB(5, 20, 5, 0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Stack(
//fit: StackFit.loose,
//alignment: Alignment.center,
children: [
// Positioned(
// top: 0,
// left: 0,
// right: 0,
Align(
alignment: Alignment(0, -1),
child: Hero(
tag: "imageHero",
child: ClipRRect(
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(30),
bottomRight: Radius.circular(30),
),
child: Image.asset(
'assets/images/punjabi_egg_curry1.jpeg',
//height: screenHeight * 0.3,
//width: double.infinity,
alignment: Alignment.topCenter,
),
),
),
),
// ),
Center(
child: new ClipRect(
child: new BackdropFilter(
filter:
new ImageFilter.blur(sigmaX: 10.0, sigmaY: 10.0),
child: new Card(
color: Colors.transparent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
child: new Center(
//child: GestureDetector(
//onTap: _updateSize,
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Column(
children: [
AnimatedContainer(
constraints: BoxConstraints(
maxHeight: maxHeight * 0.85),
height: _height,
duration: Duration(milliseconds: 300),
decoration: new BoxDecoration(
color: Colors.transparent,
borderRadius: BorderRadius.circular(20),
//border: Border.all(color: Colors.black),
),
child: Steps(),
),
//),
ElevatedButton(
onPressed: _updateSize,
child: Text(buttonText),
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(
vertical: 10,
horizontal: 20,
),
primary: kSilver,
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(20),
),
onPrimary: Colors.black,
textStyle: TextStyle(
color: Colors.black,
fontSize: 22,
),
),
),
],
),
),
),
),
),
),
),
// ),
],
),
],
),
),
),
),
//),
);
}
}
Wrap that widget with Positioned(top:5, child: // your widget ),
Try below code hope its help to you change your image on your need
Container(
height: 500,
child: Stack(
children: [
Container(
width: 150,
height: 150,
margin: EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage(
'assets/images/cycle.png',
),
),
),
),
Positioned(
top: 120,
left: 0,
right: 0,
child: Container(
height: 100,
child: ListView.builder(
itemCount: 20,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text("List - $index"),
);
},
),
),
),
],
),
),
Your Result Screen ->

How to blur image in Container without blurring children Flutter?

I only want the image of container blurred (line:16). This is the code:
itemBuilder: (BuildContext context, int index) {
return Stack(
children: <Widget>[
Container(
margin: EdgeInsets.only(top: 40, bottom: 20),
width: 250,
height: 450,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10)),
boxShadow: [
BoxShadow(
blurRadius: 8,
offset: Offset(0, 2),
),
],
image: DecorationImage(
image: AssetImage(
Category_list[index]["background"]), //Blur this image
fit: BoxFit.cover),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
Category_list[index]["header"],
style: TextStyle(
fontSize: 30,
color: Colors.white,
fontWeight: FontWeight.w800,
),
),
),
Padding(
padding: const EdgeInsets.all(12.0),
child: Container(
width: 250,
height: 50,
//color: Colors.white,
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
BorderRadius.all(Radius.circular(10)),
),
child: TextButton(
onPressed: () => {},
child: Text(
"Chat",
style: TextStyle(color: Colors.black),
),
),
),
),
],
),
),
],
);
But to use BackdropFilter you have to use the child-property from the container but this would blur everything. Does anybody know how to only blur the image? Do I have to separate the image-widget from the rest? And if so how does it still cover the complete Stack?
You can use Stack() widget to show your title or anything on top of your blurred image.
Small sample example
Container(
height: 150,
width:150,
decoration: new BoxDecoration(
color:Colors.black,
image: new DecorationImage(
image: new NetworkImage('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQKgi8mXs-MtRnwuZVUKo1BwTXnKIl--6qZPzWmT4k3BV1wlN-vMgV2BdICGGHlekwnkVk&usqp=CAU'),
fit: BoxFit.cover,
),
),
child: Stack(
children:[
new BackdropFilter(
filter: new ImageFilter.blur(sigmaX: 2.0, sigmaY: 2.0),
child: new Container(
decoration: new BoxDecoration(color: Colors.white.withOpacity(0.0)),
),
),
Text("Dog Name",style: TextStyle(fontSize: 22,),),
],
),
);

Flutter: Transparent child with fill parent's background color

I am just trying to display the camera's preview and align the user to take a selfie.
I have the following snippet of code and I would like to make the person's image transparent (not white) and fill the surrounding area with color.
but the problem that I am facing, if I set the SVG with transparent color, it will display the color of the parent (container),
I need to make the person's image totally transparent to see the camera's preview with filling the surrounding area with color.
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Stack(
children: [
Container(
height: MediaQuery.of(context).size.height,
child: Center(
child: _cameraPreviewWidget(),
),
),
Container(
height: MediaQuery.of(context).size.height,
color: Colors.grey[700].withOpacity(0.8),
child: Align(
alignment: Alignment.bottomCenter,
child: SvgPicture.asset(
'assets/svg/selfie_person.svg',
alignment: Alignment.bottomCenter,
fit: BoxFit.cover,
color: Colors.white,
),
),
),
Container(
height: MediaQuery.of(context).size.height,
child: Padding(
padding: EdgeInsets.only(
top: MediaQuery.of(context).size.height * 0.05,
bottom: MediaQuery.of(context).size.height * 0.15,
),
child: Column(
children: [
ListTile(
leading: InkWell(
onTap: () {
Navigator.pop(context, null);
},
child: FaIcon(
FontAwesomeIcons.arrowLeft,
color: Colors.white,
),
),
title: Center(
child: Text(
"Take a selfie",
style: Theme.of(context).textTheme.subtitle2,
),
),
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: MediaQuery.of(context).size.width * 0.05,
),
child: Text(
"Take a quick selfie so we know it's you, this is never public",
style: Theme.of(context).textTheme.subtitle2,
overflow: TextOverflow.ellipsis,
maxLines: 3,
textAlign: TextAlign.center,
),
),
Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: _captureButton(),
),
),
],
),
),
),
],
),
),
);
I don't understand your question well, but i think you should try Opacity class .. wrap your SVG with Opacity
You can check it :
https://api.flutter.dev/flutter/widgets/Opacity-class.html

How do I round the edges of this panel in Flutter?

I am using the https://pub.dev/packages/flutter_sliding_up_panel dependency and I want to round the edges of the panel below.
This is the code I am using
SlidingUpPanelWidget(
child: Column(
children: <Widget>[
ClipRRect(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(35.0),
topRight: Radius.circular(35.0),
),
child: Container(
color: Colors.red,
alignment: Alignment.center,
child: Row(
children: <Widget>[
Icon(
Icons.menu,
size: 30,
),
Padding(
padding: EdgeInsets.only(
left: 8.0,
),
),
Text(
'click or drag',
)
],
mainAxisAlignment: MainAxisAlignment.center,
),
height: 50.0,
),
),
Divider(
height: 0.5,
color: Colors.grey,
),
Flexible(
child: Container(
child: ListView.separated(
controller: scrollController,
physics: ClampingScrollPhysics(),
itemBuilder: (context, index) {
return ListTile(
title: Text('list item $index'),
);
},
separatorBuilder: (context, index) {
return Divider(
height: 0.5,
);
},
shrinkWrap: true,
itemCount: 20,
),
color: Colors.white,
),
),
],
mainAxisSize: MainAxisSize.min,
),
controlHeight: 50.0,
anchor: 0.4,
panelController: panelController,
),
and this is what the collapsed panel currently looks like:
I want the entire panel to be as rounded as the red container.
You can't do it. The library has a hardcoded Material widget around the widget you give it.
The only thing you can do is to copy the whole library and modify it. It is a small library only has one file you need to copy.
At line 192 in the library you have this code:
child: Material(
key: _childKey,
color: Theme.of(context).backgroundColor,
elevation: widget.elevation,
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: widget.child,
),
),
Modify it like this:
child: Material(
key: _childKey,
color: Colors.transparent,
shadowColor: Colors.transparent,
elevation: widget.elevation,
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: widget.child,
),
),

DecorationImage/Image as Child going outside of Container?

I have tried these
1. DecorationImage
2. Image
3. CircleAvatar
I have used Stack as there will be a background image.
But I am not able to understand why the image is expanding like that.
I need a circular image for the profile pic.
If I use Column instead of ListView then the images doesn't expand like this ...but I need a ListView.
The code:-
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 80.0,
width: 80.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
image: AssetImage(Assets.cool),
fit: BoxFit.cover,
),
color: Colors.red,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 50.0,
width: 50.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
),
child: Image(
image: AssetImage(Assets.cool),
fit: BoxFit.cover,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
radius: 50.0,
backgroundImage: AssetImage(Assets.cool),
),
)
],
)
],
),
);
}
From my understanding you want the image in a circle not a ellipse. You can achieve that by wrapping the items in your listview with a Row element.
return Scaffold(
body: Stack(
children: <Widget>[
ListView(
children: <Widget>[
// Wrap with a row
Row(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 80.0,
width: 80.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
image: AssetImage("a.jpg"),
fit: BoxFit.cover,
),
color: Colors.red,
),
),
),
//...
],
),
],
)
],
),
);
To get a circular image, Just wrap all your widget of ListView in a Column widget and place the Column in ListView. You'll get a scrollable column.
ListView(
children: <Widget>[
Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 80.0,
width: 80.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
image: AssetImage(Assets.cool),
fit: BoxFit.cover,
),
color: Colors.red,
),
),
),
//.....
],
)
],
),s

Resources