Container does not get max width inside a Wrap widget? - flutter

I have a Container widget inside a Wrap widget, and I want the Container to have the maximum width inside the Wrap, but when I use width: double.infinity for the width of the Container, it throws an error. How can I achieve my target? The code is below:
Container(
width: 150,
color: Colors.grey.shade300,
child: Wrap(
direction: Axis.vertical,
children: [
Text("Hi there!"),
Text("This is a very long long string to show!"),
Container(
color: Colors.teal,
height: 5,
width: double.infinity,
),
Text("The end."),
],
),
),

Use a row to get full width
Container(
width: 150,
color: Colors.grey.shade300,
child: Wrap(
direction: Axis.vertical,
children: [
Text("Hi there!"),
Text("This is a very long long string to show!"),
Row(
children: [
Container(
color: Colors.teal,
height: 5,
),
]),
Text("The end."),
],
),
),

You've define the top level width, To have the same width, you can just put it inside the container instead of adding widgets. like
Container(
width: 150, //from here
color: Colors.grey.shade300,
child: Wrap(
direction: Axis.vertical,
children: [
Text("Hi there!"),
Text("This is a very long long string to show!"),
Container(
color: Colors.teal,
height: 5,
width: 150, //to here
),
Text("The end."),
],
),
),

Related

How to center one element and place the second element next to it

In my layout I have two Widgets in a row, a text and a button.
How can I achieve something like below, where only the Text is centered, and the icon is simply next to it?
---------------------------
Text *
---------------------------
Using Row would center all the contents and would output something like
---------------------------
Text *
---------------------------
Tried: Row(children:[widget1, widget2], mainAxisAlignment: MainAxisAlignment.center);
But this centers both items, causing the text to look off-centered.
You can use CompositedTransformTarget and CompositedTransformFollower as mentioned on comment section by pskink.
class AppPT extends StatelessWidget {
const AppPT({super.key});
#override
Widget build(BuildContext context) {
final LayerLink _layerLink = LayerLink();
return Scaffold(
body: Column(
children: [
Stack(
children: [
Align(
child: CompositedTransformTarget(
link: _layerLink,
child: Container(
color: Colors.red,
width: 100,
height: 60,
alignment: Alignment.center,
child: Text("btn"),
),
),
),
CompositedTransformFollower(
link: _layerLink,
targetAnchor: Alignment.centerRight,
followerAnchor: Alignment.centerLeft,
child: Container(
color: Colors.cyanAccent,
width: 12,
height: 12,
alignment: Alignment.center,
child: Text("*"),
),
),
],
)
],
),
);
}
}
There are few tricks I can think of,
You can use Stack widget with Position.
including another widget on right by applying opacity(invisible) on current snippet.
using transform will be handle if you know the width of the widget.
Transform.translate(
offset: Offset(20 / 2, 0), //20 is the * box size
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
color: Colors.red,
width: 100,
height: 60,
alignment: Alignment.center,
child: Text("btn"),
),
Container(
color: Colors.green,
width: 20,
height: 20,
child: Center(child: Text("*")),
),
],
),
),
Place text and second widget inside row then put the row inside container with alignment center and use SizedBox for spacing between widget instead of Padding Widget.
Container(
color: Colors.green,
alignment: Alignment.center,
height: 100,
child: Row(
mainAxisSize: MainAxisSize.min,
children: const [
Text("Text"),
SizedBox(width: 10),
Text("*"),
],
),
);

Flutter: Clip a Column with Expanded when overflow

My use case is:
There are 3 widgets in a Column: widgets 1 and 3 are fixed height, and widget 2 will expand the available content.
Container(
height: 300, // h0: dynamic height
child: Column(
children: [
Container(
color: Colors.green,
width: 100, // h1: fixed height
height: 100,
),
Expanded(
child: Container(
color: Colors.red,
width: 100, // h2: fill remain content
child: Placeholder(),
),
),
Container(
color: Colors.blue,
width: 100, // h3: fixed height
height: 100,
),
],
),
)
When h0 > h1 + h3:
Show all 3 widgets -> Passed
When h0 < h1 + h3:
Show widgets 1 and 3, the widget in Expanded will be invisible -> Passed
Clip the content if overflow -> Failed the content is not clipped and a warning Bottom overflow
I cannot use ScrollView because the content in Expanded should be flexible which ScrollView does not support behavior like that.
I can use ClipRect to clip the overflow content but the warning Bottom overflow still remains. Is there a way to dismiss the warning?
Try wrapping Column with SingleChildScrollView:
Container(
height: 300, // h0: dynamic height
child: CustomScrollView(
slivers: [
SliverFillRemaining(
hasScrollBody: false,
child: Column(
children: [
Container(
color: Colors.green,
width: 100, // h1: fixed height
height: 100,
),
Expanded(
child: Container(
color: Colors.red,
width: 100, // h2: fill remain content
child: Placeholder(),
),
),
Container(
color: Colors.blue,
width: 100, // h3: fixed height
height: 100,
),
],
),
),
],
),
),
You can try it :
Container(
height: 300,
child: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
color: Colors.green,
width: 100, // h1: fixed height
height: 100,
),
Flexible(
fit: FlexFit.loose,
child: Container(
color: Colors.red,
width: 100, // h2: fill remain content
child: Placeholder(),
),
),
Container(
color: Colors.blue,
width: 100, // h3: fixed height
height: 100,
),
],
),
),
),

Image is not fitted when used in leading of ListTile

The asset image is 200x200px, but when it used in leading of ListTile it's not fitted as axpected.
The rendered image looks this way. How to display it proportioanlly 90x90 as it's defined in Image height and width properties?
Card(
key: ValueKey(favorites[index]),
margin: EdgeInsets.all(20),
child: ListTile(
leading: kNoImage,
title: Text(favorites[index]),
subtitle: Text(favorites[index]),
trailing: IconButton(
icon: Icon(Icons.close),
onPressed: () {
debugPrint('entry deleted');
},
),
),
);
const kNoImage = Image(
image: AssetImage('assets/no-user-image.png'),
height: 90,
width: 90,
fit: BoxFit.cover,
);
Change the value of fit to something like BoxFit.scaleDown or BoxFit.contain to get the desired result. See the BoxFit docs to see what these values mean.
The max height for leading widget for your case is ≈56px.
So, if you want to explicitly want to keep it 90, I'd suggest you to make your own ListTile with Row() and Column() which is pretty easy.
This is what I found in ListTile(),
final Offset baseDensity = visualDensity.baseSizeAdjustment;
if (isOneLine)
return (isDense ? 48.0 : 56.0) + baseDensity.dy;
if (isTwoLine)
return (isDense ? 64.0 : 72.0) + baseDensity.dy;
return (isDense ? 76.0 : 88.0) + baseDensity.dy;
I prefer not to use ListTile because of it default behavior. Therefor, I prefer
on dartPad or on Gist
Custom ListTile
Card(
elevation: 8,
child: Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(defaultBorderRadiusCircular),
),
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 6),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
SizedBox(
width: 90.0,
height: 90.0,
child: ClipRRect(
borderRadius:
BorderRadius.circular(defaultBorderRadiusCircular),
child: Image.asset(
'imagePath',
width: 90,
height: 90,
fit: BoxFit.cover,
),
),
),
const SizedBox(
width: 24,
),
Column(
children: [
const Text(
'Danau Beretan',
),
const Text(
'Indonesia',
),
],
),
],
),
Row(
children: [
Icon(Icons.star_half_outlined),
Text("4.5"),
],
)
],
),
),
),

Flutter design with timeline

I'm trying to make this drawing but I'm struggling.
I don't understand how to make the white block of information dynamic. That is to say that it automatically adapts to the size in height of the elements it contains.
I am for the moment obliged to give a defined height (here 200), I would like the size to be automatic. Moreover if I do not give a defined size my gray line on the left side disappears.
The design :
ListTile(
title : Column(
children: [
Container(
width: double.infinity,
child: Row(
children: [
Container(
width: 50,
alignment: Alignment.center,
child: Container(
width: 20,
height: 20,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
border: Border.all(
width: 5,
color: Colors.blue,
style: BorderStyle.solid
)
),
),
),
Text('9:00 am - 9:15 am')
],
)
),
Container(
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
width : 50,
alignment: Alignment.center,
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
color: Colors.grey,
width: 3,
height : 200 // if I remove the size it desappear
),
],
),
),
),
Expanded(
child: Container(
margin: EdgeInsets.symmetric(vertical :10),
padding: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
color: Colors.white,
child: Column(
children: [
Text('Titre'),
Text('Info 1'),
Text('Info 2')
],
),
),
)
],
),
),
)
],
),
),
You can try this package for timelines https://pub.dev/packages/timeline_tile.
This package also automatically Sizes your widget height and width.
And for Automatically Sizing your widgets next time, You could use Expanded widget at most places in your code.
Note: An Expanded widget must be a descendant of a Row, Column, or Flex, So Use it carefully.
I think it's much easier with this package, timelines.
TimelineTileBuilder.connected(
connectionDirection: ConnectionDirection.before,
itemCount: processes.length,
contentsBuilder: (_, index) {
//your Container/Card Content
},
indicatorBuilder: (_, index) {
if (processes[index].isCompleted) {
return DotIndicator(
color: Color(0xff66c97f),
child: Icon(
Icons.check,
color: Colors.white,
size: 12.0,
),
);
} else {
return OutlinedDotIndicator(
borderWidth: 2.5,
);
}
},
connectorBuilder: (_, index, ___) => SolidLineConnector(
color: processes[index].isCompleted ? Color(0xff66c97f) : null,
),
),
),

How to put view above the list view?

I have a listview which is equally divided into two parts(act as a background) How can i put a view above it( 100*100 w*h )?(This box should center align & top of root)
here is my tried code -
#override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
Container(
width: ((context.findRenderObject() as RenderBox).size.width),
height: ((context.findRenderObject() as RenderBox).size.height)/2,
color: darkGreen,
),
// SizedBox(width: 10,),
Container(
width: ((context.findRenderObject() as RenderBox).size.width),
height: ((context.findRenderObject() as RenderBox).size.height)/2,
color: Colors.grey,
),
],
),
);
}
}
First of all, you don't need this line:
((context.findRenderObject() as RenderBox).size.width)
you can replace it with:
double.infinity
and it will take the maximum width the parent Widget can allow.
as for your question, you can wrap your ListView in a Stack:
Container(
width: double.infinity,
height: double.infinity,
child: Stack(
children: <Widget>[
ListView(
children: <Widget>[
Container(
width: double.infinity,
height:
((context.findRenderObject() as RenderBox).size.height) / 2,
color: darkGreen,
),
// SizedBox(width: 10,),
Container(
width: double.infinity,
height:
((context.findRenderObject() as RenderBox).size.height) / 2,
color: Colors.grey,
),
],
),
//place your desired widget here
],
),
),