First of all, the solution could be obvious given that I'm a newbie in Flutter, but I've searched for many hours to no avail, so on to the issue. I'm trying to have a markdown description in Flutter, which should be able to be expanded when tapping a chevron.
I have tried AnimatedContainer
, AnimatedSize
, Flex
with Expanded
and also tried looking into the warning "It should only be called from paint callbacks or interaction event handlers (e.g. gesture callbacks)."
Flutter gives me if I try to get the size of the child before it's painted.
I've taken a look to several places, including StackOverflow questions that I'll link right here:
- Flutter Layout Row / Column - share width, expand height
- https://github.com/flutter/flutter/issues/16061
- How to get a height of a Widget?
The next bit of code is where I'm trying to get the animation of expanding/collapsing going.
The variable isExpanded
is just a boolean and the function _onPressed
just toggles a change in that boolean.
Based on it I should get the markdown body in its total height or just show a bit of it.
@override
Widget build(BuildContext context) {
markdown = Markdown(body);
var icon = (isExpanded) ? Icons.expand_less : Icons.expand_more;
var iconButton = IconButton(icon: Icon(icon), onPressed: _onPressed);
return Container(
child: Column(children: <Widget>[
AnimatedContainer(
// constraints: BoxConstraints(), this is where I'd like to set the change from 30% to infinity
child: SingleChildScrollView(child: markdown),
curve: Curves.bounceIn,
duration: Duration(milliseconds: 300),
),
iconButton
]),
);
}
What I'm trying to do is a Trello-like animation. In the phone app, the description of a card is expandable and collapsable. When expanded, it takes just the height it needs, even overflowing the screen. This means the scrollable part is the Widget that contains the description and whatever other widgets there are, not the description itself.
What actually happens is:
- If I try to set a height (in
BoxConstraints
) in withMediaQuery
and then expand toSize.infinity
animation can't be actually handled because it goes from bounded to unbounded. - If I try to set the parameter
height
in theAnimatedContainer
to 30% of the screen and then to infinity, when it's infinity the content just disappears.