skip to Main Content

I understand that it is used to adjust AppBar, but what is PreferredSize after all?
What is the use of PreferredSize widget in flutter?

The following text is found in the official documents, but I do not understand what it means.
https://api.flutter.dev/flutter/widgets/PreferredSize-class.html

It just advertises a preferred size which can be used by the parent.

The explanation in the official documentation is also limited to the AppBar, which I could not understand further.

I wrote the following code as a test, but the height of the green container was spread across the entire screen.

import 'package:flutter/material.dart';

void main() {
  runApp(const _MyApp());
}

class _MyApp extends StatelessWidget {
  const _MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: PreferredSize(
            preferredSize: const Size.fromHeight(100.0),
            child: Container(
              width: 300,
              color: Colors.green,
            ),
          ),
        ),
      ),
    );
  }
}

2

Answers


  1. Here is the Short Details about it:


    Preferred Size is a custom widget lets you allow to design your custom appbar for you with the same height, width, elevation and feel similar to Appbar.
    Sometimes you want to create tabs or more effective design for your appbar then you can create a customChild for your appBar with the help of PreferredSizeWidget.

    Login or Signup to reply.
  2. The size this widget would prefer if it were otherwise unconstrained.
    In many cases it’s only necessary to define one preferred dimension.
    For example the [Scaffold] only depends on its app bar’s preferred
    height. In that case implementations of this method can just return
    Size.fromHeight(myAppBarHeight).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search