I am trying to implement my own "expandable panel" in Flutter, basically there are questions and their answers, all having different length naturally. What i want to do is to display the question in a container first, and when user clicks the container i want to expand the second container's height, which has the answer text, from 0 to the height value it's answer requires
double _theHeight = 0;
Column(
children: [
Container(
child:Inkwell(
onTap: () {setState({_theHeight = ????})},
child: Text(theQuestion)
)
),
Container(
height = _theHeight,
child: Text(theAnswer),
),
]
)
In such example, i tried to give _theHeight variable a constant value like 200 but as different answers have different lengths, this causes an overflow or too much unnecessary place.
Is there a default value for height such that the parent widget will only cover the space it's child requires? So can i type something like:
_theHeight = Container.default.height;
Thank you for your time.
CodePudding user response:
You could simply wrap the answer inside the container. No need to give specific height. You can just check if the widget is expanded or not
Example:
bool isExpanded = false;
Column(
children: [
Container(
child:Inkwell(
onTap: () {setState({isExpanded = !isExpanded})},
child: Text(theQuestion)
)
),
if(isExpanded)
Container(
child: Text(theAnswer),
),
]
)
For animated container, you could use AnimatedSize something like
AnimatedSize(
duration: const Duration(seconds: 1),
child: isExpanded ? Container() : const Text(theAnswer),
),