How to get height or width of a Widget in flutter?

This Article is posted by seven.srikanth at 11/21/2022 1:19:55 AM



You can get the heigh or width of a widget in flutter by using layout builder. 
Below is the output of a quick example of how you can get those values using layout builder.
Here is the complete code from main.dart file.

import 'package:flutter/material.dart';
void main(List<String> args) {
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @ override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: MyLayoutWidget(),
      ),
    );
  }
}
class MyLayoutWidget extends StatelessWidget {
  const MyLayoutWidget({
    super.key,
  });
  @ override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        color: Colors.grey,
        height: 300,
        width: 300,
        child: LayoutBuilder(
          builder: (p0, p1) {
            return Center(
                child:
                    Text("height = ${p1.maxHeight}, width = ${p1.maxWidth}"));
          },
        ),
      ),
    );
  }
}
Thanks,
Srikanth

Tags: Height; width; constraints; parent widget;








0 Comments
Login to comment.
Recent Comments













© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com