Skip to content

Instantly share code, notes, and snippets.

@BarryDaBee
Created November 22, 2022 06:35
Show Gist options
  • Save BarryDaBee/9565ef8450ed16af1dbd13fe62468ed6 to your computer and use it in GitHub Desktop.
Save BarryDaBee/9565ef8450ed16af1dbd13fe62468ed6 to your computer and use it in GitHub Desktop.
Demonstration of how to get the size of a widget and delay its rendering by one frame
class ResizingCard extends StatefulWidget {
const ResizingCard({Key? key, this.onClose}) : super(key: key);
final VoidCallback? onClose;
@override
State<ResizingCard> createState() => _ResizingCardState();
}
class _ResizingCardState extends State<ResizingCard>
with TickerProviderStateMixin {
final _widgetKey = GlobalKey();
///Ignore this and other parts related to animation
final height = ValueNotifier<double?>(null);
/// Hide widget
bool _showWidget = false;
@override
void initState() {
SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
final box = _widgetKey.currentContext!.findRenderObject()! as RenderBox;
height.value = box.size.height;
print(height.value);
setState(() {
///Show widget after first has rendered;
_showWidget = true;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Visibility(
visible: _showWidget,
/// Maintain state, size and animation of the hidden widget.
maintainState: true,
maintainSize: true,
maintainAnimation: true,
child: Container(
key: _widgetKey,
height: height.value,
padding: const EdgeInsets.all(20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: const [
Text(
'Lorem Ipsum is simply dummy text of '
'the printing and typesetting industry.'
'Lorem Ipsum has been the industry\'s'
'standard dummy text ever since the 1500s, '
'when an unknown printer took a galley of type and '
'scrambled it to make a type specimen book'
'It has survived not only five centuries'
'but also the leap into electronic typesetting'
'remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum',
),
SizedBox(height: 22),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment