1

I need to calculate the height of a child widget before its parent renders.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Material App',
      home: Scaffold(
        appBar: CustomAppbar(
          widget: Text(
            'This is a tex',
          ),
        ),
      ),
    );
  }
}

The above code just call a Widget CustomAppbar, which receives as parameter a TEXT widget

class CustomAppbar extends StatelessWidget implements PreferredSizeWidget {
  final Widget? widget;

  const CustomAppbar({
    Key? key,
    this.widget,
  }) : super(key: key);

  @override
  Size get preferredSize => const Size.fromHeight(130.0);

  @override
  Widget build(BuildContext context) {
    return ClipPath(
      clipper: HeaderCustomClipper(),
      child: ColoredBox(
        color: Color.fromRGBO(255, 191, 14, 1),
        child: Column(
          children: [
            _AppBar(),
            if (widget != null) widget as Widget,
          ],
        ),
      ),
    );
  }
}

The above code just is a customAppbar, I want the height of the child widget (widget) to be captured before the CustomAppbar renders.

If I pass a text, it should be seen like that:

enter image description here

If I don't pass anything, it should be seen like that:

enter image description here

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Labrador
  • 417
  • 2
  • 5
  • 18
  • Does this answer your question? [How to get height of a Widget?](https://stackoverflow.com/questions/49307677/how-to-get-height-of-a-widget) – Vega Mar 30 '22 at 05:47
  • No, that post does not allow apply it to my case, because y a property of the app bar: ```Size get preferredSize => const Size.fromHeight(130.0);``` – Labrador Mar 31 '22 at 14:20

1 Answers1

0

to achieve that you can use GlobalKey -> BuildContext -> RenderBox -> widget-> global position & rendered size.

please check this below link

https://stackoverflow.com/a/49650741/17180860

Jinto Joseph
  • 947
  • 6
  • 23
  • Please flag the question as duplicate, do not answer – Vega Mar 30 '22 at 05:47
  • i know but as help i Answered it , so after seeing my post , he will understand that question is already on this site , so i thik he will remove the question – Jinto Joseph Mar 30 '22 at 06:59