Search
 
SCRIPT & CODE EXAMPLE
 

DART

flutter get child widget size

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

typedef OnWidgetSizeChange = void Function(Size size);

/// [MeasuredSize] Calculated the size of it's child in runtime.
/// Simply wrap your widget with [MeasuredSize] and listen to size changes with [onChange].
class MeasuredSize extends StatefulWidget {
  /// Widget to calculate it's size.
  final Widget child;

  /// [onChange] will be called when the [Size] changes.
  /// [onChange] will return the value ONLY once if it didn't change, and it will NOT return a value if it's equals to [Size.zero]
  final OnWidgetSizeChange onChange;

  const MeasuredSize({
    Key? key,
    required this.onChange,
    required this.child,
  }) : super(key: key);

  @override
  _MeasuredSizeState createState() => _MeasuredSizeState();
}

class _MeasuredSizeState extends State<MeasuredSize> {
  bool _calculating = true;
  @override
  void initState() {
    SchedulerBinding.instance!.addPostFrameCallback(postFrameCallback);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    SchedulerBinding.instance!.addPostFrameCallback(postFrameCallback);

    return AnimatedOpacity(
      duration: const Duration(milliseconds: 100),
      opacity: _calculating ? 0.0 : 1.0,
      child: Container(
        key: widgetKey,
        child: widget.child,
      ),
    );
  }

  var widgetKey = GlobalKey();
  Size? oldSize;

  void postFrameCallback(_) {
      _calculating = true;
      
    var context = widgetKey.currentContext!;
    
    // await Future.delayed(
    //     Duration(milliseconds: 100)); // wait till the image is drawn
    
    Size newSize = context.size!;
    _calculating = false;
    if (newSize == Size.zero) return;
    if (oldSize == newSize) return;
    oldSize = newSize;
    widget.onChange(newSize);
  }
}
Comment

PREVIOUS NEXT
Code Example
Dart :: how to store special characters in dart string 
Dart :: how to create space between list on flutter 
Dart :: How use late in Dart 
Dart :: how to update listview in flutter 
Dart :: flutter text padding 
Dart :: dart anonymous function in forEach 
Dart :: what is the difference between runapp() and main() in flutter 
Dart :: how to acces parameter value from stataful widget flutter 
Dart :: link failed but did not provide an info log flutter 
Dart :: how to get current timezone flutter 
Dart :: nullable conditional assignment dart 
Dart :: how to groupby list of maps in flutter 
Dart :: var dump print object flutter dart 
Dart :: flutter run future builder only 1 time 
Dart :: flutter const advantag 
Dart :: flutter center title ignore button 
Dart :: dart uzunlik 
Swift :: swiftui width screen 
Swift :: play sound in swift 5 
Swift :: add buton border swift 
Swift :: remove divider list swiftui 
Swift :: swiftui navigationview ignore top space 
Swift :: swift wait 5 seconds 
Swift :: change button text in swift 
Swift :: swiftui rectangle color 
Swift :: and in swif 
Swift :: swift string to dictionary 
Swift :: swift get "system" asset image 
Swift :: swift screenshot 
Swift :: objective c vs swift 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =