close
How to get height of a Widget in Flutter, get height of a Widget in Flutter, get height of a Widget, width of a widget in flutter, height of a widget in flutter

How to get height of a Widget in Flutter?

Hello Guys How are you all? Hope You all are fine. I want to know my widget Height and width. How to get height of a Widget in a flutter. So today Here I come with all possible solutions for this error.

We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.

How to get height of a Widget in Flutter?

  1. How to get height of a Widget in Flutter?

    To get height of a widget in flutter Here's a sample on how you can use LayoutBuilder to determine the widget's size. Since LayoutBuilder widget is able to determine its parent widget's constraints, one of its use case is to be able to have its child widgets adapt to their parent's dimensions.

  2. get height of a Widget in Flutter

    To get height of a widget in flutter Here's a sample on how you can use LayoutBuilder to determine the widget's size. Since LayoutBuilder widget is able to determine its parent widget's constraints, one of its use case is to be able to have its child widgets adapt to their parent's dimensions.

Solution 1

The easiest way is to use MeasuredSize it’s a widget that calculates the size of it’s child in runtime.

You can use it like so:

MeasuredSize(
          onChange: (Size size) {
            setState(() {
              print(size);
            });
          },
          child: Text(
            '$_counter',
            style: Theme.of(context).textTheme.headline4,
          ),
        );

Solution 2

Here’s a sample on how you can use LayoutBuilder to determine the widget’s size.

Since LayoutBuilder widget is able to determine its parent widget’s constraints, one of its use case is to be able to have its child widgets adapt to their parent’s dimensions.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  var dimension = 40.0;

  increaseWidgetSize() {
    setState(() {
      dimension += 20;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(children: <Widget>[
          Text('Dimension: $dimension'),
          Container(
            color: Colors.teal,
            alignment: Alignment.center,
            height: dimension,
            width: dimension,
            // LayoutBuilder inherits its parent widget's dimension. In this case, the Container in teal
            child: LayoutBuilder(builder: (context, constraints) {
              debugPrint('Max height: ${constraints.maxHeight}, max width: ${constraints.maxWidth}');
              return Container(); // create function here to adapt to the parent widget's constraints
            }),
          ),
        ]),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: increaseWidgetSize,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

Summery

So, It’s All About This tutorial. I hope this tutorial helps you to Solve your error. Please Comment Below if You stucks anywhere with my code. And please comment below on which solution worked for you. Thank You.

Also Check Out Below Tutorials


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *