Skip to main content

Command Palette

Search for a command to run...

FractionallySizedBox widget in Attributes

Published
2 min read
FractionallySizedBox widget in Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

The FractionallySizedBox widget in Flutter is a flexible container that sizes its child widget based on a fraction of the available space in its parent widget. This widget is particularly useful when you want to create responsive layouts or specify dimensions relative to the available screen size.

Attributes of the FractionallySizedBox widget typically include:

  1. widthFactor: A double value between 0.0 and 1.0 that represents the fraction of the parent's width that the child should occupy.

  2. heightFactor: A double value between 0.0 and 1.0 that represents the fraction of the parent's height that the child should occupy.

  3. alignment: An optional parameter that specifies how the child widget should be aligned within the available space.

Now, let's dive into an example of how to use the FractionallySizedBox widget in a Flutter application:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'FractionallySizedBox Widget Example',
      home: FractionallySizedBoxDemo(),
    );
  }
}

class FractionallySizedBoxDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('FractionallySizedBox Widget Example'),
      ),
      body: Center(
        child: Container(
          color: Colors.grey,
          width: 300,
          height: 300,
          child: FractionallySizedBox(
            widthFactor: 0.5,
            heightFactor: 0.5,
            alignment: Alignment.center,
            child: Container(
              color: Colors.blue,
              child: Center(
                child: Text(
                  'FractionallySizedBox Example',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 20.0,
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

In this example, we've created a Flutter application with a FractionallySizedBoxDemo widget. Inside this widget, we have a Container with a fixed width and height of 300 pixels. Within the Container, we've placed a FractionallySizedBox widget as its child. This FractionallySizedBox widget occupies half the width and half the height of its parent Container (since widthFactor and heightFactor are both set to 0.5). The child of the FractionallySizedBox is another Container with a blue background color and text displaying "FractionallySizedBox Example", which is centered horizontally and vertically within the FractionallySizedBox.

This example demonstrates how the FractionallySizedBox widget can be used to create responsive layouts by specifying dimensions relative to the available space.

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"