How to control image size in container? - flutter

Inside a Card there is one column,which contain 2 Flexible widgets (with flex property 3,1) each one contain a Container widget
here is the code:
#override
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
child: Card(
child: Column(
children: [
Flexible(
flex: 3,
child: Container(
color: Colors.red,
)
),
Flexible(
flex: 1,
child: Container(
color: Colors.amber,
child: Center(child: Text('Request')
),
)
)
],
),
),
);
this code give me the below screen:
Now i want to place image to cover all the red area,
#override
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
child: Card(
child: Column(
children: [
Flexible(
flex: 3,
child: Container(
color: Colors.red,
child: Image.network(
'https://placeimg.com/640/480/any', fit: BoxFit.cover,)
)
),
Flexible(
flex: 1,
child: Container(
color: Colors.amber,
child: Center(child: Text('Request')
),
)
)
],
),
),
);
}
but when add image inside Container this is what happen
i tried to change fit: BoxFit.cover, but nothing happen
how to make image cover Container(with red area) without change the container size?
Adding Row and other column
Widget build(BuildContext context) {
return Container(
color: Colors.blue,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Card(
child: Column(
children: [
Flexible(
flex: 3,
child: Container(
// use the decoration property
decoration: BoxDecoration(
color: Colors.red,
// image here
image: DecorationImage(
image: NetworkImage(
'https://placeimg.com/640/480/any',
),
fit: BoxFit.cover,
),
),
),
),
Flexible(
flex: 1,
child: Container(
color: Colors.amber,
child: Center(
child: RichText(
text: TextSpan(
text: 'Request a service',
style: TextStyle(
fontSize: 20,
color: Colors.black
)
),
)
),
)
)
],
),
),
Card(
child: Column(
children: [
Flexible(
flex: 3,
child: Container(
// use the decoration property
decoration: BoxDecoration(
color: Colors.red,
// image here
image: DecorationImage(
image: NetworkImage(
'https://placeimg.com/640/480/any',
),
fit: BoxFit.cover,
),
),
),
),
Flexible(
flex: 1,
child: Container(
color: Colors.amber,
child: Center(
child: RichText(
text: TextSpan(
text: 'Request a service',
style: TextStyle(
fontSize: 20,
color: Colors.black
)
),
)
),
)
)
],
),
),
],
),
);
}

Use the decoration property of the Container to give it an Image instead:
Flexible(
flex: 3,
child: Container(
// use the decoration property
decoration: BoxDecoration(
color: Colors.red,
// image here
image: DecorationImage(
image: NetworkImage(
'https://placeimg.com/640/480/any',
),
fit: BoxFit.cover,
),
),
),
),
RESULT:
EDIT: "Why is my Image not showing when I use it in a Row" ?:
You need to give your Card widget a Width by wrapping in a SizedBox or a Container.
Added a demo using your code as an example:
class Help extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.blue,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
SizedBox(
width: 150, // give width here
child: Card(
child: Column(
children: [
Expanded(
flex: 3,
child: Container(
// use the decoration property
decoration: BoxDecoration(
color: Colors.red,
// image here
image: DecorationImage(
image: NetworkImage(
'https://placeimg.com/640/480/any',
),
fit: BoxFit.cover,
),
),
),
),
Flexible(
flex: 1,
child: Container(
color: Colors.amber,
child: Center(
child: RichText(
text: TextSpan(
text: 'Request a service',
style: TextStyle(
fontSize: 20, color: Colors.black)),
)),
))
],
),
),
),
SizedBox(
width: 150, // give width here
child: Card(
child: Column(
children: [
Expanded(
flex: 3,
child: Container(
// use the decoration property
decoration: BoxDecoration(
color: Colors.red,
// image here
image: DecorationImage(
image: NetworkImage(
'https://placeimg.com/640/480/any',
),
fit: BoxFit.cover,
),
),
),
),
Flexible(
flex: 1,
child: Container(
color: Colors.amber,
child: Center(
child: RichText(
text: TextSpan(
text: 'Request a service',
style: TextStyle(
fontSize: 20, color: Colors.black)),
)),
))
],
),
),
),
],
),
),
);
}
}
NOTE: It would be advisable to give the Card a width based on the Device screen.
RESULT:

Related

How to fill a second container color?

I have this design:
And I want that the bottom of the container also have a pink color, something like this:
This is my code:
LayoutBuilder(
builder: (context, constraints) {
return Scaffold(
backgroundColor: Color(0xffF6F6F6),
body: TextSelectionTheme(
data: TextSelectionTheme.of(context).copyWith(
selectionColor: Color(0xffD7D7D7),
cursorColor: Color(0xff3B3B3B),
selectionHandleColor: Color(0xffD7D7D7),
),
child: Center(
child: Container(
height: double.maxFinite,
width: double.maxFinite,
decoration: BoxDecoration(
color: Color(0xffF6F6F6),
borderRadius: BorderRadius.all(
Radius.circular(0.0),
),
),
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
RichText(
text: TextSpan(
style: GoogleFonts.questrial(),
children: [
WidgetSpan(
child: Icon(FontAwesomeIcons.checkDouble,
size: 40,
color: Color(0xffD7D7D7)),
),
],
),
),
RichText(
text: TextSpan(
style: GoogleFonts.questrial(),
children: [
WidgetSpan(
child: GestureDetector(
onTap: () {
showDialog(
context: context,
barrierColor: Colors.transparent,
builder: (ctx) => HomePagePomodoroTimer());
},
child: Icon(FontAwesomeIcons.squareXmark,
size: 40,
color: Color(0xff3B3B3B),),
),
),
],
),
),
],
),
),
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Center(
child: Container(
decoration: BoxDecoration(
color: Color(0xffF4CFDD),
),
child: Padding(
padding: const EdgeInsets.fromLTRB(50, 30, 50, 0),
child: Column(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"We send you an Email",
style: TextStyle(
fontSize: 20,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
color: Color(0xff3B3B3B),
),
),
],
),
SizedBox(height: 50,),
const Center(
child: Text(
"Please, check your Email inbox to log in and start using Pomoworko",
style: TextStyle(
fontSize: 20,
fontStyle: FontStyle.normal,
fontWeight: FontWeight.normal,
color: Color(0xff3B3B3B),
),
),
),
Divider(color: Color(0xff3B3B3B)),
SizedBox(height: 40,),
const SizedBox(height: 360,
child: RiveAnimation.asset('letter_and_knife.riv',
fit: BoxFit.cover
),),
],
),
),
),
),
],
)
],
),
),
),
),
),
);
}
)
If I add this piece of code to the pink container:
Center(
child: Container(
height: double.maxFinite,
decoration: BoxDecoration(
color: Color(0xffF4CFDD), //pink color
),
I got this:
How to solve this issue?
It is possible to add a second container at the same time or not?
Thank you in advance
The problem is that you are using height: double.maxFinite with returns constant value of 1.7976931348623157e+308, in that case you want the size of your current widget parent context simple use MediaQuery.of(context).size.height
the code should look like this:
Center(
child: Container(
height: MediaQuery.of(context).size.height,
decoration: const BoxDecoration(
color: Color(0xffF4CFDD),
),
Consider giving the two children equal height or height you would want them to have from the constraints using SizedBox or Container like below.
LayoutBuilder(
builder: (context, constraints) {
return Column(
children: [
Container(
height: constraints.maxHeight/2, width: double.infinity, color: Colors.red,
),
Container(
height: constraints.maxHeight/2, width: double.infinity, color: Colors.red,
),
]
);
});
This way now you can move the children you want inside those containers.
Note: SingleChildScrollView does not work with infinite constraints children like Column on infinite height, consider sizing them or setting MainAxisSize.min or Flexible instead of constraints.

Texts visibly stacked on each other in flutter

Widget build(BuildContext context) {
return Swipable(
child: Column(
children: [
Container(
height: MediaQuery.of(context).size.height * 0.57,
child: Card(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(14),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(14),
child: Image.network(
widget.urls,
fit: BoxFit.cover,
),
),
),
),
),
Center(
child: Expanded(
child: Text(
widget.usernames,
maxLines: 1,
)),
)
],
),
//onSwipeLeft: () => _returnString(index),
);
}
}
This is my code and i've been adjusting and readjusting for over 3hrs.. I just need the text to show on the image that can be swiped. Please help me. Thanks
image:
Hello I think what you are needing is to use a Stack Widget instead of a Column widget. When using Stacks you can use a Positioned widget to indicate exactly where you want your child widgets to be displayed inside of the stack as well.
Widget build(BuildContext context) {
return Swipable(
child: Stack(
alignment: Alignment.center,
children: [
Container(
height: MediaQuery.of(context).size.height * 0.57,
child: Card(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(14),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(14),
child: Image.network(
widget.urls,
fit: BoxFit.cover,
),
),
),
),
),
Positioned(
top: 16,
right: 16,
child: Center(
child: Expanded(
child: Text(
widget.usernames,
maxLines: 1,
)),
),
)
],
),
//onSwipeLeft: () => _returnString(index),
);
}
}
Happy Coding!
You can use this way
Widget build(BuildContext context) {
return Column(
children: [
Container(
decoration: BoxDecoration(
image: DecorationImage(
widget.urls,
fit: BoxFit.cover,
),
),
height: MediaQuery.of(context).size.height * 0.57,
width: MediaQuery.of(context).size.height,
child: Align(
alignment: Alignment.bottomCenter,
child: Text(
widget.usernames,
style: TextStyle(color: Colors.orange),
),
),
),
],
);
//onSwipeLeft: () => _returnString(index),
}

White space between Expanded widgets

Some Flex values create white space between Expanded widgets, as I understand it is the remainder of the calculations.
For example:
Scaffold(
body: Column(
children: <Widget>[
Expanded(
flex: 4,
child: Container(
color: Colors.green,
),
),
Expanded(
flex: 6,
child: Container(
color: Colors.green,
),
),
],
),
);
I'm looking for an elegant way to make these widgets take up all the available space.
The whole problem is that the flex values change during animation and this white line blinks.
Thanks!
it seems it is coming from border of Container,
Github Issue is still open.
here is the widget
Scaffold(
backgroundColor: Colors.green,
body: Column(
children: <Widget>[
Expanded(
flex: 4,
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.green,
border: Border.all(
color: Colors.green,
),
),
child: Text("Section A"),
),
),
Expanded(
flex: 6,
child: Container(
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.green,
border: Border.all(
color: Colors.green,
),
),
child: Text("Section B"),
),
),
],
),
);
Try to add SizedBox() in between your expanded Widget like below:
Scaffold(
body: Column(
children: <Widget>[
Expanded(
flex: 4,
child: Container(
color: Colors.green,
),
),
SizedBox(
height: 25,)
Expanded(
flex: 6,
child: Container(
color: Colors.green,
),
),
],
),
);

How to make Container height by depend on length of text flutter

i want to know, how let the container height depend on length of text and cover it in container not in picture 2 if i setting text too long the text is come over container then how to fix it
on message by b-akused02 : it's short and still in height :100,
on message by b-akused01 : it's long over container
on message by b-akused03 : it's short and still in height :100,
Widget _buildCommentItem({#required CommentDetail commentDetail}) {
return Container(
height: 100,
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SizedBox(
height: 20.0,
),
Expanded(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Container(
height: 45, //height, //155,
width: 45, //width, //155,
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: DecorationImage(
image: NetworkImage(
commentDetail.otherUserProfileImageUrl),
fit: BoxFit.cover,
),
borderRadius: BorderRadius.circular(12),
),
),
),
Expanded(
flex: 8,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Wrap(
// crossAxisAlignment: CrossAxisAlignment.start,
children: [
FittedBox(
fit: BoxFit.cover,
child: Text(
commentDetail.otherUsername,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold),
),
),
Text(
commentDetail.comment,
style: TextStyle(
color: Colors.black,
),
),
],
),
),
),
],
),
),
),
SizedBox(
height: 20,
)
],
),
),
);
}
Wrap the given container in a Column and remove the height parameter from within.
Complete Code
Widget _buildCommentItem({#required CommentDetail commentDetail}) {
return Container(
child: Container(
child: Wrap(
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
// crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Container(
height: 45, //height, //155,
width: 45, //width, //155,
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: DecorationImage(
image: NetworkImage(
commentDetail.otherUserProfileImageUrl),
fit: BoxFit.cover,
),
borderRadius: BorderRadius.circular(12),
),
),
),
Expanded(
flex: 8,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Wrap(
// crossAxisAlignment: CrossAxisAlignment.start,
children: [
FittedBox(
fit: BoxFit.cover,
child: Text(
commentDetail.otherUsername,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold),
),
),
Text(
commentDetail.comment,
style: TextStyle(
color: Colors.black,
),
),
],
),
),
),
],
),
),
),
],
),
),
);
}
only create a Container without height.
Container(
width: 200, //for example
child: Text('any Text'),
)

Adding an image background to home page

I am trying to add a background to my home page, however, it does not seem to be working. Any thoughts? I know how to add it per se, but unsure of how to structure it. I had received some advice to use a Stack, but I am unfamiliar with this method and the resources online are not clear.
Any advice on how to structure this would be appreciated.
Here is the background image code:
Widget build(BuildContext context) {
return DecoratedBox(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/startbackground.jpg"),
fit: BoxFit.cover),
),
and here is the page code itself.
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.orangeAccent,
height: double.infinity,
width: double.infinity,
alignment: Alignment.bottomLeft,
child: UnconstrainedBox(
child: Padding(
padding: const EdgeInsets.only(left: 50, bottom: 50),
child: Container(
height: 400,
width: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Container(
child: Text(
"Text",
style: TextStyle(
fontSize: 30, fontWeight: FontWeight.w700),
textAlign: TextAlign.left,
),
color: Colors.purpleAccent,
),
),
Expanded(
flex: 3,
child: Container(
child: Text(
"Some Text",
style: TextStyle(
fontSize: 60, fontWeight: FontWeight.w700),
),
color: Colors.purpleAccent,
),
),
Expanded(
flex: 3,
child: Container(
child: Text(
"Some Text",
style: TextStyle(
fontSize: 60, fontWeight: FontWeight.w700),
),
color: Colors.teal,
),
),
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.all(15),
child: FlatButton(
minWidth: 200,
onPressed: () {},
child: Text(
"Get Started",
style:
TextStyle(color: Color(0xff7638c9), fontSize: 15),
),
color: Colors.transparent,
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.purple),
borderRadius: BorderRadius.circular(18.0),
),
),
),
),
],
),
),
),
),
),
);
}
}
It's confusing, but we don't actually need a DecoratedBox, rather a BoxDecoration (inside your container, for the decoration: argument).
:P
Here's an example:
class BackgroundImagePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(
color: Colors.orangeAccent, // this has been moved up into BoxDeco
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'https://i.pravatar.cc/300'
)
)
),
height: double.infinity,
width: double.infinity,
... <snip> ...
If the Scaffold is a child of the background image code, than I guess the Scaffold background color is hiding the image.
Set transparent background color for the Scaffold:
Scaffold(
backgroundColor: Color.transparent,
...
This ended up working. See below.
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Stack(children: <Widget>[
Image.asset(
"assets/startbackground.jpg",
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
fit: BoxFit.cover,
),
Scaffold(
body: Container(
color: Colors.transparent,
height: double.infinity,
width: double.infinity,
alignment: Alignment.bottomLeft,
child: UnconstrainedBox(
child: Padding(
padding: const EdgeInsets.only(left: 50, bottom: 50),
child: Container(
height: 400,
width: 200,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Container(
child: Text(
"Text",
style: TextStyle(
fontSize: 30, fontWeight: FontWeight.w700),
textAlign: TextAlign.left,
),
color: Colors.transparent,
),
),
Expanded(
flex: 3,
child: Container(
child: Text(
"Some Text",
style: TextStyle(
fontSize: 60,fontWeight: FontWeight.w700),
),
color: Colors.transparent,
),
),
Expanded(
flex: 3,
child: Container(
child: Text(
"Some Text",
style: TextStyle(
fontSize: 60, fontWeight: FontWeight.w700),
),
color: Colors.transparent,
),
),
Expanded(
flex: 2,
child: Padding(
padding: EdgeInsets.all(15),
child: FlatButton(
minWidth: 200,
onPressed: () {},
child: Text(
"Get Started",
style: TextStyle(
color: Color(0xff7638c9), fontSize: 15),
),
color: Colors.transparent,
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.purple),
borderRadius: BorderRadius.circular(18.0),
),
),
),
),
],
),
),
),
),
),
)
]);
}
}