diff --git a/lib/linear_percent_indicator.dart b/lib/linear_percent_indicator.dart index 6790aa5..255550a 100644 --- a/lib/linear_percent_indicator.dart +++ b/lib/linear_percent_indicator.dart @@ -7,6 +7,16 @@ extension ExtDouble on double { bool get isZero => this.toString() == '0.0'; } +extension ExtGlobalKey on GlobalKey { + Size getSize() { + final RenderObject? renderObject = currentContext?.findRenderObject(); + if (renderObject is RenderBox && renderObject.hasSize) { + return renderObject.size; + } + return Size.zero; + } +} + // ignore: must_be_immutable class LinearPercentIndicator extends StatefulWidget { ///Percent value between 0.0 and 1.0 @@ -177,13 +187,14 @@ class _LinearPercentIndicatorState extends State void initState() { WidgetsBinding.instance.addPostFrameCallback((_) { if (mounted) { + final Size containerSize = _containerKey.getSize(); + final Size indicatorSize = _keyIndicator.getSize(); setState(() { - _containerWidth = _containerKey.currentContext?.size?.width ?? 0.0; - _containerHeight = _containerKey.currentContext?.size?.height ?? 0.0; + _containerWidth = containerSize.width; + _containerHeight = containerSize.height; if (_keyIndicator.currentContext != null) { - _indicatorWidth = _keyIndicator.currentContext?.size?.width ?? 0.0; - _indicatorHeight = - _keyIndicator.currentContext?.size?.height ?? 0.0; + _indicatorWidth = indicatorSize.width; + _indicatorHeight = indicatorSize.height; } }); }