How to align an item to the bottom of a list? - flutter

I'm trying to make an item go to the bottom of a list. I've tried using Expanded with a child of SizedBox. I've also tried the Align widget. It works with a Column, but not if the parent is a SingleChildScrollView.
What it should look like:
What it does look like:
Code (what it does look like):
return Scaffold(
backgroundColor: Color(0xFFE9E9E9),
body: ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16),
children: <Widget>[
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
),
child: _SettingsList(tournamentId: tournamentId),
),
Expanded(child: const SizedBox()),
Align(
alignment: FractionalOffset.bottomCenter,
child: buildDeleteTournamentButton(),
),
],
),
);

I think the problem is that you are using the Expanded or Align widget as a child of the list view so it tries to put the widget directly below the other ones. To achieve the first design I would recommend one of this two ways:
Replace your ListView with a Stack and use Align to align your "Delete tournament"-Widget at the bottom.
Replace your ListView with a Column and wrap the Container in an Expanded. Place your "Delete tournament"-Widget as the second child of the Column.
I really hope I could help.

Related

Flutter - How to wrap list of chipsets with a button horizontally together

I'm new to flutter and have been trying to get a list of chipsets wrapped in a Wrap widget with the ability to add more of them through a button shown in the image. However, the button isn't able to wrap along with the chipset horizontally. Below is an image of the design to make it clearer.
Code:
Container(
child: Wrap(
children: [
Container(
child: Wrap(
children: _chipsetList,
),
),
SizedBox(width: 2),
Container(
height: 35,
width: 35,
child: FittedBox(
child: FloatingActionButton(
heroTag: null,
elevation: 0,
shape: StadiumBorder(
side: BorderSide(width: 2.0)),
onPressed: () {
_getMoreChips(context);
},
child: Icon(Icons.add),
),
))
],
),),
I tried to experiment with SingleChildScrollView property or with Wrap direction but in vain. What could be a way to get this design working?
Seems you want to put chips and the button in the single wrap. To do so, combine them in a single list and use this list as wrap's children
List<Widget> items = List.from(_chipsetList); // copy
Widget button = FloatingActionButton();
items.add(button);
Wrap(
children: items,
)
Or, using spread operator
Wrap(
children: [
..._chipsetList,
FloatingActionButton(),
],
)

Column widget draws a line between children when they are wrapped in specific widget with decoration. How can i cure this?

The problem is visible on the attached screenshot.
Code of a main widget:
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Flexible(
child: Decor(
child: Container(),
),
flex: 1,
),
Flexible(
child: Decor(
child: Container(),
),
flex: 6,
)
],
);
Code of a Decor widget:
Container(
decoration: BoxDecoration(
color: COLOR,
),
margin: const EdgeInsets.symmetric(
horizontal: MEDIUM_PADDING,
),
padding: const EdgeInsets.all(MEDIUM_PADDING),
width: 300.0,
child: child,
);
Answering in advance "Why would you even need to do such a Decor Widget"? Both flexibles will be populated with data of some sort. Second flexible will be with a ListView inside and the first one will be a "Header" for what is inside a ListView. Data is retrieved from server every 10s, so the main widget is wrapped with a StreamBuilder.
Problem is that StreamBuilder cannot be shrinked to the size of its child, whereas ListView can, so I wrap both "Header" and a ListView in Decor so that grey background color doesn't take all available space on a screen which than produces black line seen on a screenshot?
So the question is: is there a way either to remove the black line between two widgets in a column?
Also, if you know magic of how to shrink StreamBuilder to the size of its child please answer here too.
Ok I know that this is not a very nice solution and more like workaround but I think the only way to get rid of the line is to set border color of the container in your Decor widget
Container(
decoration: BoxDecoration(
color: COLOR,
border: Border.all(width: 0, color: COLOR) //added line
),
margin: const EdgeInsets.symmetric(
horizontal: MEDIUM_PADDING,
),
padding: const EdgeInsets.all(MEDIUM_PADDING),
width: 300.0,
child: child,
);
First I thought setting border width to 0 will help it but that didn't work.. Color did

Making a scrollable flat button in Flutter application

I'm trying to embed a flat button with a variable amount of text within a scroll view, so that the user can scroll the text but also tap it in order to perform an action. I tried doing this with a flat button embedded in a ConstrainedBox, which itself is embedded in a SingleChildScrollView.
I've tried embedding the FlatButton in a SingleChildScrollView as below. Earlier, I tried wrapping the text in an expanded widget with a SingleChildScrollView ancestor but that caused runtime errors because the requirements of the scroll view and the expanded view conflict (as far as I understand).
Widget contentScreen() {
return SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(),
child: FlatButton(
onPressed: () {
_toggleContent();
},
child:
Column(children: <Widget>[
Container(
child: Text(
"Lorem Ipsum....",
style: TextStyle(color: Colors.white, fontSize: 20))),
]
)
)
)
);
}
The text just doesn't scroll. Instead it overflows and shows the diagonal yellow bars. I don't have a list of exactly what I've tried, but where I'm at right now is that I'm using the above code but there is no scrolling behavior as expected. :\
I tried this: How to make the Scrollable text in flutter?
This: Make scrollable Text inside container in Flutter
And this: how to make text or richtext scrollable in flutter?
Is there something about the FlatButton's behavior that just precludes scrolling? If so, how can I work around that to still get the two behaviors (ability to both scroll and tap to perform action) that I want?
Have you tried this? Why do you need Expanded widget?
Container(
height: 20.0,
child: FlatButton(
onPressed: () {},
child: SingleChildScrollView(
child: Text('Lorem ipsum'),
),
),
),
Gesture Detector should also work well.
Container(
height: 20.0,
child: FlatButton(
onPressed: () {},
child: SingleChildScrollView(
child: Text('Lorem ipsum'),
),
),
),

Alignment of widgets ignored in row

I'm creating a new widget that displays some bubbles, that should be connected by lines.
5-------6
|
4-------3
|
1-------2
I chose a ListView as the outer widget, if I understood it correctly this should give me the scrollability:
return Expanded(
child: ListView(
children: _createLWItems(_createBubbles()),
),
);
Children is a List of my inner widget. Each widget is a row, so I can place two bubbles next to each other:
return Container(
child: Row(
children: <Widget>[
Container(
child: bubbleItem1,
alignment: Alignment.centerLeft,
),
Container(
child: bubbleItem2,
alignment: Alignment.centerRight,
)
],
),
decoration: BoxDecoration( //TODO remove after testing
shape: BoxShape.rectangle,
border: Border.all(color: Colors.blue),
),
);
BubbleItem is only a Container with a BoxDecoration wrapped with a GestureDetector.
What I wanted to achieve is, that the left bubble is aligned to the left, and the right to the right, but instead, both are placed next to each other to the left, although row takes the entire width of my screen, as you can see (blue border):
As I read here there is a MainAxisAlignment for the row-widget but this would just align all childs left, right, centered, etc., not what I have in mind.
How can I place those bubbles the way I intend to?
If they are placed correctly I want to connect them with lines. Is there a way to do this without calculation? My idea would be to insert another widget between the bubbles that takes a line, but I'd need to tell it somehow, that it should take the entire space between the bubbles of his row.
At the moment I'm a bit unsure if I should calculate anything because I read something about 'you cannot calculate before it is rendered', but on the other hand I'm missing some understanding how to do it in flutter without it.
Alignment does work for Row children, but not in the way that you are expecting. If for example you had a Row child with half the height of the Row, you could use Alignment to specify if you want that child at the top or the bottom of the Row - the default is centered.
By default, the children are positioned sequentially within a Row (horizontally) as well as within a Column (vertically), which is what you've got in the screenshot you attached. If you simply wanted to position the 2 circles at the left and right ends of the Row, you could use a MainAxisAlignment value of spaceBetween, which places the free space evenly between the children.
If you want to also draw a line between the circles however, then you will not have any space left to distribute within the Row, so the MainAxisAlignment will not help. What you want instead is an Expanded widget within the Row, between the circles. This will create a widget that takes up all the remaining space within the Row, which is where you want your line to be.
Row(
children: <Widget>[
Container(
child: bubbleItem1,
alignment: Alignment.centerLeft,
),
Expanded(
child: Divider(),
),
Container(
child: bubbleItem2,
alignment: Alignment.centerRight,
)
],
)
You are giving the Alignment to the container's item, not the container itself
Try giving the Alignment to the Row
Container getContainer(){
return Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
height: 50,
width: 50,
child: Center(child: Text("Left")),
),
Container(
height: 50,
width: 50,
child: Center(child: Text("Right")),
)
],
),
decoration: BoxDecoration( //TODO remove after testing
shape: BoxShape.rectangle,
border: Border.all(color: Colors.blue),
),
);
}

Make container widget fill parent vertically

TL;DR Need the container to fill the vertical space so that it can act as a ontap listener. Have tried most solutions but nothing seems to work.
So what I am trying to do is to make my container fill up the vertical space while still having a fixed width. Two first is what I have and third is what I want. The idea is to have the container transparent with a gesture ontap listener. If anyone have a better idea as for a different solution, feel free to suggest.
Widget build(BuildContext context) {
return new GestureDetector(
onHorizontalDragUpdate: _move,
onHorizontalDragEnd: _handleDragEnd,
child: new Stack(
children: <Widget>[
new Positioned.fill(
child: new Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
new Container(
child: new IconButton(
padding: new EdgeInsets.only(top: 16.0, bottom: 16.0, left: 24.0, right: 24.0),
icon: new Icon(Icons.warning),
color: Colors.black12,
onPressed: () {},
)
),
],
),
),
new SlideTransition(
position: new Tween<Offset>(
begin: Offset(0.0, 0.0),
end: const Offset(-0.6, 0.0),
).animate(_animation),
child: new Card(
child: new Row(
children: <Widget>[
new Container(
width: 20.0,
height: 20.0,
color: Colors.amber,
),
new Expanded(
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_getListTile(),
_ifStoplineIsToBeShown()
],
),
)
],
)
),
),
],
)
);
}
I am quite sure that i have been missing something considering the fact that I have tried a lot of different things and nothing seems to work.
I have also uploaded an image with the debug painting here.
PS. I know I have set the height to a fixed value, but this is the only way to show the container.
The trick is to combine an IntrinsicHeight widget and a Row with crossAxisAlignment: CrossAxisAlignment.stretch
This force the children of Row to expand vertically, but Row will take the least amount of vertical space possible.
Card(
child: IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Container(
width: 20.0,
color: Colors.amber,
),
// Expanded(...)
],
),
)
)
To stretch the container to full height of the parent use property constraints:BoxConstraints.expand() in container widget. Container occupy the complete space independent of the of child widget
Container(
color: Colors.green,
child: Text("Flutter"),
constraints: BoxConstraints.expand(),
)
Please refer the link Container Cheat sheet for more about container
Simply pass in: double.infinity.
If you want a Container to fill all available space, you can just pass in:
width: double.infinity,
height: double.infinity
Explanation:
In Flutter, a child widget cannot exceed the "layout constraints" imposed by its parent widget. During the layout phase, Flutter engine uses a constraint solver to automatically correct "out-of-bound" values into what's allowed by its parent constraints.
For example, if you have a Container that's 50x50, and for its child, you pass in another Container that's 300x300, the inner container will be automatically corrected to "not exceed its parent", thus 50x50. Therefore, using sufficiently large values would always make sure you "fill parent".
In fact, even BoxConstraints.expand() exploits the same idea internally. If you open up the source code of expand(), you will see:
/// Creates box constraints that expand to fill another box constraints.
///
/// If width or height is given, the constraints will require exactly the
/// given value in the given dimension.
const BoxConstraints.expand({
double width,
double height,
}) : minWidth = width ?? double.infinity,
maxWidth = width ?? double.infinity,
minHeight = height ?? double.infinity,
maxHeight = height ?? double.infinity;
So if you are absolutely certain you want to fill all spaces, you can intuitively pass in a number bigger than the parent (or larger than the whole screen), like double.infinity.
As of Jan 2020 the simplest is to use an Expanded Widget
Expanded(flex: 1,
child: Container(..),
),
https://api.flutter.dev/flutter/widgets/Expanded-class.html
There are many answers which suggest using two things
constraints: BoxConstraints.expand(),
height: double.infinity,
But both these answer will give you an error like
BoxConstraints forces an infinite height.
We can avoid these by calculating the height of the screen like
App Bar
Top Bar Space(Exist on the above App Bar)
Remaining screen
1. Get the MediaQuery
final mediaQuery = MediaQuery.of(context);
2. Declare the AppBar Widget and same App Bar instance should be used in Scaffold App Bar
final PreferredSizeWidget appBar = AppBar(
title: Text('Home'),
);
3. Use calculated height
Container(
width: mediaQuery.size.width,
height: (mediaQuery.size.height -
appBar.preferredSize.height -
mediaQuery.padding.top),
color: Colors.red,
),
Output:
Set the height or width of a container to double.maxFinite
Container(
height: double.maxFinite,
width: 100,)
You can make your widget take the full size of a Container widget, and then set the container's height and/or width to double.maxFinite. This will make the Container take the height and/or width or its parent widget
I propose using Expanded widget (which allows us to avoid IntrinsicHeight widget), combine it with the Container's alignment property and therefore make it work properly even if the Container is not the only one at the screen.
Expanded(
child: Container(
alignment: Alignment.center,
child: Text('Your text', textAlign: TextAlign.center))),
That way one also avoids potential app's crash which occurs often when you accidentally expand to infinity some parts of the widget tree both horizontally and vertically (that is why you are not able to use BoxConstraints widget in many cases).
One can read more about the problems of passing constraints in Flutter here - a must read: https://medium.com/flutter-community/flutter-the-advanced-layout-rule-even-beginners-must-know-edc9516d1a2
This work works for me
height: MediaQuery.of(context).size.height,