Why is expanded container not visible within list view? - flutter

I have a widget that displays a 100x100 image and a blue container next to it which fills the rest of the Row.
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
Scaffold.of(context).showSnackBar(SnackBar(content: Text(title)));
},
child: Container(
padding: EdgeInsets.all(24),
child: Row(
children: [
SizedBox(
child: Image.asset("assets/img_small.png"),
width: 100,
height: 100),
Expanded(
child: Container(margin: EdgeInsets.only(left: 12), color: Colors.blue)
)
],
),
));
}
This works fine but as soon as I use it within a list view, the blue container is not visible:
#override
Widget build(BuildContext context) {
return ListView.separated(
itemCount: list.length,
separatorBuilder: (context, index) =>
Divider(color: Colors.grey, thickness: 2, height: 0, indent: 8),
itemBuilder: (context, position) {
return BlueBoxWidget(title: list[position]);
});
}
What am I doing wrong?
I am mostly interested why inside a listview the blue container doesn't try to expand its height as much as possible.

The blue Container is there, but with 0 height.
Try adding some height, like this, to get the container visible
Expanded(
Container(
height: 50,
margin: EdgeInsets.only(left: 12),
color: Colors.blue
),
)

try to remove the Expanded widget that wrapped the Container it maybe work.

Related

Using ListView with scrollDirection set to Axis.horizontal inside a SimpleDialog throws an error

When i try to use a ListView which contains checkboxes with scrollDirection set to Axis.horizontal inside a SimpleDialog throws an error: RenderShrinkWrappingViewport does not support returning intrinsic dimensions. I tried wrapping the ListView with Containers, Flexible.. but it still thorws an error.
If i set the scrollDirection to Axis.vertical it works fine. I am guessing the problem is with it being inside a dialog.
#override
Widget build(BuildContext context) => SimpleDialog(
backgroundColor: Color.fromARGB(255, 229, 233, 240),
contentPadding: EdgeInsets.zero,
children: [
Expanded(
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children: [
...personCheckboxes.map(buildCheckboxes).toList(),
],
),
),
The buildCheckboxes function:
Widget buildCheckboxes(CheckBoxState checkbox) => CheckboxListTile(
controlAffinity: ListTileControlAffinity.leading,
activeColor: Colors.blue,
value: checkbox.checked,
title: Text(checkbox.title, style: const TextStyle(fontSize: 12)),
onChanged: (value) => setState(() {
checkbox.checked = value!;
if (checkbox.value == 'M') {
maleChecked = checkbox.checked;
} else if (checkbox.value == 'F') {
femaleChecked = checkbox.checked;
}
checkResults();
setState(() {});
//runFilterCheckbox(checkbox.value, value);
}),
);
I have tried wrapping the ListView widget with different widgets (Container). It still produces an error.
Widget build(BuildContext context) => SimpleDialog(
backgroundColor: Color.fromARGB(255, 229, 233, 240),
contentPadding: EdgeInsets.zero,
children: [
Container(
height: 100.0,
width: 100.0,
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children: [
...personCheckboxes.map(buildCheckboxes).toList(),
],
),
),
Even after wrapping the listView with SizedBox, the problem persist:
#override
Widget build(BuildContext context) => SimpleDialog(
backgroundColor: Color.fromARGB(255, 229, 233, 240),
contentPadding: EdgeInsets.zero,
children: [
SizedBox(
height: 20.0,
width: double.maxFinite,
child: ListView(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
children: [
...personCheckboxes.map(buildCheckboxes).toList(),
],
),
),
Wrap your Listview in a Sizedbox and give it some height to it and add width as width: double.maxFinite,. It should work after you add width as AlertDialog uses an IntrinsicWidth widget that does not allow ListView.builder.
Use Alert Dialog instead of using SimpleDialog. Check below code for alertdialog implementation:
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Container(
width: double.maxFinite,
child: ListView(
children: <Widget>[]
),
),
);
}
);
On horizontal list and CheckboxListTile are trying to get infinite width. You can wrap the CheckboxListTile with SizedBox widget and provide width. You can take help of MediaQuery.of(context).size.width. And for the ListView It also required height and width on case and can be used SizedBox with height and width.
SizedBox(
height: MediaQuery.of(context).size.height*.5,
width: 200, //or
child: ListView(
buildCheckboxes(...)=> SizedBox(
width: 200,
child: CheckboxListTile(
controlAffinity: ListTileControlAffinity.leading,
Also you can follow other widget like AlertDialog. or just
showDialog(
context: context,
builder: (context) => SizedBox(
height: MediaQuery.of(context).size.height * .5,
width: 200, //or
child: Material(..

How to set dynamic height for Container Widget in flutter?

I have text widget inside of container and if text is more than 4 lines this is overflowing to bottom of container. How can I set dynamic height for this container that will depends of text lines and avoid bottom overflow ? At the moment container have fixed value but if I remove this value it will disappear.
home component
Widget build(BuildContext context) {
return Container (
child: SliverToBoxAdapter(
child: Container(
height: 180,
margin: EdgeInsets.only(top: 10),
child: CustomeInfoWidget(
infoData: _infoDataList,
),
),
),
)
}
CustomeInfoWidget
Widget build(BuildContext context) {
final double _width = MediaQuery.of(context).size.width;
return Container(
margin: EdgeInsets.only(top: 15, left: 15, right: 15),
child: Stack(
children: [
PageView.builder(
itemCount: widget._infoDataList.length,
controller: _pageController,
itemBuilder: (BuildContext context, int index) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: _width - 10,
child: Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Text(widget._infoDataList[index].title,
textAlign: TextAlign.left,
),
Container(
child: Text(
widget._infoDataList[index].content,
),
)
],
);
},
),
],
),
);
}
wrap your container with Singlechildscrollview or set overflow for your Text widget Flutter - Wrap text on overflow, like insert ellipsis or fade
On your home component, you are fixing the height of CustomeInfoWidget. You can remove this.
SliverToBoxAdapter(
child: Container(
// height: 180,// remove this
margin: EdgeInsets.only(top: 10),
child: CustomeInfoWidget(
You can use SliverFillRemaining for PageView.
SliverFillRemaining(
child: PageView.builder(
Or you can wrap your text container with Singlechildscrollview for inner scroll.
You can also calculate the text height and provide it.
How can I get the size of the Text widget in Flutter

Flutter Semantics gets stuck in nested lists

I am trying to run the Xcode Accessibility Inspector over my Flutter app and have issues when I have a vertical list that has a nested horizontal list. The screen reader gets stuck on the horizontal list and either never progresses or drops focus. I expected the reader to read off each square but observed the reader getting stuck on horizontal list. I have tried to create an example app to demo the issue.
Here is the build function of my stateless widget:
Widget _getVertList(double width) {
return ListView.builder(
shrinkWrap: true,
itemBuilder: (_, i) {
if (i != 0 && i % 3 == 0) {
return _getHorzList(width); // show horizontal list
}
return Semantics(
label: 'RED BOX $i',
child: Column(
children: [
Container(
color: Colors.red,
height: 100,
width: 100,
),
const SizedBox(
width: 10,
height: 10,
)
],
),
);
},
itemCount: 20,
);
}
Widget _getHorzList(double width) {
return SizedBox(
height: 101,
width: width,
child: Column(
children: [
Expanded(
child: ListView.builder(
shrinkWrap: true,
physics: const ClampingScrollPhysics(),
scrollDirection: Axis.horizontal,
itemBuilder: (_, i) {
return Semantics(
label: 'Blue BOX $i',
child: Row(
children: [
Container(
color: Colors.blue,
height: 100,
width: 100,
),
const SizedBox(
width: 10,
height: 10,
)
],
),
);
},
itemCount: 10,
),
),
],
),
);
}
#override
Widget build(BuildContext context) {
final double width = MediaQuery.of(context).size.width;
return Scaffold(
body: _getVertList(width, height));
}

Flutter Stack not aligning floating action button

I'm making a listview with a few buttons. I've used a stack widget to place two floating action buttons on top of Listview. However, I want to align them to the bottom right and bottom left of the listview. I'm not able to do this we Align widget although I don't understand why. Note that I understand I could've used a scaffold widget here but that is not an option given Listview is a child of Scaffold. Also, I've tried using the alignment property of Stack, but it didn't work. I was hoping you guys could help solve this problem :D
This is the code here:
#override
Widget build(BuildContext context) {
#override
void initState() {
super.initState();
written = [];
read = [];
}
return Stack(
//fit: StackFit.expand,
alignment: AlignmentDirectional.bottomEnd,
children: [
ListView.separated(
reverse: true,
shrinkWrap: true,
separatorBuilder: (context, i) => Container(
//color: Colors.greenAccent,
width: double.infinity,
height: 8,
),
itemCount: savedhistory.length,
itemBuilder: (context, i) => Dismissible(
key: UniqueKey(),
onDismissed: (direction) {
setState(() {
savedhistory.removeAt(i);
updateString(i);
});
},
child: Container(
margin: EdgeInsets.all(10),
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: Colors.grey,
borderRadius: BorderRadius.all(Radius.circular(20))),
child: savedhistory[i],
//padding: EdgeInsets.all(8.0),
),
/* read == null
? []
: read.map((e) => ExpansionTile(
title: Text(e),
subtitle: Text(e),
)), */
)),
Positioned(
//alignment: Alignment.bottomRight,
// child: GestureDetector(
// onLongPress: () => getString(),
bottom: 50,
right: 0,
child: FloatingActionButton(
child: Icon(Icons.replay_outlined),
onPressed: getString,
backgroundColor: Colors.green[200])),
Align(
alignment: Alignment.bottomLeft,
child: Container(
child: FloatingActionButton(
child: Icon(Icons.download),
onPressed: addString,
backgroundColor: Colors.green[200]),
),
)
]);
}
}
After using positioned there seems to be some space beyond which button gets clipped, I don't understand why though.
[1]: https://i.stack.imgur.com/bjrnL.png
try to use Positioned widget :
stack(
children[
Positoned(
bottom : 0
left :1
child: Container(
/// YOUR CODE ///
)
)
]
)
first set stack widget's alignment to bottomcenter and then use Positoned() for first button widget with left: 0 and with second button right:0

flutter: How to achieve the gradually fading text in a ListView?

I want to achieve the style like the picture.
A ListView containing a few Container, and each container has some text. When scrolling the ListView, I want to gradually faded part of the content of the last Container. The last means the one displayed in the bottom of the ListView.
this is what I have done. Some explanation: timeTable is a widget in front of the ListView, and statisticItem is exactly the Container that I mentioned above.
body: Column(
children: <Widget>[
timeTable(),
SizedBox(height: 18,),
Expanded(
child: ListView.separated(
itemCount: 5,
separatorBuilder: (context, index) => SizedBox(height: 24.0,),
itemBuilder: (context, index) => statisticItem(),
),
)
],
)
Also, I have made some effort in shaderMask, but I can only change the background color. Any idea I'll we very appreciative.
It could be something like:
body: Column(
children: <Widget>[
timeTable(),
SizedBox(
height: 18,
),
Expanded(
child: Stack(
child: ListView.separated(
itemCount: 5,
separatorBuilder: (context, index) => SizedBox(
height: 24.0,
),
itemBuilder: (context, index) => statisticItem(),
),
FadeEndListView(),
),
)
],
),
Where FadeEndListView is:
class FadeEndListView extends StatelessWidget {
const FadeEndListView({
Key key,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Positioned(
bottom: 0,
left: 0,
right: 0,
height: 70,
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
stops: [0.0, 1.0],
colors: [
Theme.of(context).scaffoldBackgroundColor.withOpacity(0.0),
Theme.of(context).scaffoldBackgroundColor,
],
),
),
),
);
}
}
My answer is from: Fading Edge ListView - Flutter
The answer of Yariv.A. with small changes.
You can use flutter pub package.
fading_edge_scrollview
Sliver can be used. SliverList, SliverAppbar. It is used for custom scroll effects. Custom Scroll view can also be tried.
I found this question similar.
How to fade in/out a widget from SliverAppBar while scrolling?
As mentioned before by #Salim Murshed https://pub.dev/packages/fading_edge_scrollview worked for me, but I thought I'd add some of the changes you may want to make.
I altered two values within the package to achieve the desired strength of fade at the top and bottom of the list:
double gradientFractionOnStart = 0.0,
double gradientFractionOnEnd = 1.0,
Here is my test code page for this:
final _controller = ScrollController();
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
height: 190,
width: 200,
child: Column(
children: <Widget>[
Expanded(
child: FadingEdgeScrollView.fromScrollView(
child: ListView.separated(
controller: _controller,
itemCount: 50,
separatorBuilder: (context, index) => SizedBox(
height: 14.0,
),
itemBuilder: (context, index) => Text(
'${index+1} Item ${index+1}) - Test',
style: TextStyle(color: Colors.black, fontSize: 17,
fontWeight: FontWeight.w900),
),
),
),
)
],
),
),
));
}
Output display of the code