Placeholder widget and Attributes

Placeholder widget and Attributes

ยท

2 min read

The Placeholder widget in Flutter is a simple widget used to occupy space in a layout without displaying any content. It's often used as a temporary placeholder while designing a UI layout, allowing developers to visualize the arrangement of widgets without providing actual content. The Placeholder widget is typically replaced with other widgets during the development process.

Attributes:

  1. color (Color):

    • The color of the placeholder widget. It's optional and defaults to a gray color.
  2. fallbackHeight (double):

    • The fallback height of the placeholder widget. It's optional and defaults to 400.0.
  3. fallbackWidth (double):

    • The fallback width of the placeholder widget. It's optional and defaults to 400.0.

Example:

import 'package:flutter/material.dart';

class PlaceholderExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Placeholder Widget Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Placeholder(
              color: Colors.blueGrey,
              fallbackHeight: 200,
              fallbackWidth: 200,
            ),
            SizedBox(height: 20),
            Text(
              'This is a Placeholder Widget',
              style: TextStyle(fontSize: 18),
            ),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: PlaceholderExample(),
  ));
}

In this example:

  • A Placeholder widget is used inside a Column to demonstrate its usage.

  • The Placeholder widget has a specified color, fallbackHeight, and fallbackWidth, which will be used if the widget's size is not determined by the layout constraints.

  • Below the Placeholder widget, there is a Text widget providing context about the Placeholder. This text will be displayed below the placeholder in the UI.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย