Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of PreferredSize widget in flutter?

Tags:

flutter

dart

I'm new in flutter, and I see there are few widgets for the layout design such as SizedBox and Container. There is one widget which is PreferredSize Widget that I don't know and cannot find any example about it. What makes it different from other widgets such as container and SizedBox which can set height and width?.
Can someone give an example?

like image 958
esthrim Avatar asked Oct 01 '19 03:10

esthrim


People also ask

What does widget mean in Flutter?

Widgets are the central class hierarchy in the Flutter framework. A widget is an immutable description of part of a user interface. Widgets can be inflated into elements, which manage the underlying render tree. Widgets themselves have no mutable state (all their fields must be final).

How many types of widgets are there in Flutter?

Types of Widgets: There are broadly two types of widgets in the flutter: Stateless Widget. Stateful Widget.

What is the use of AppBar in Flutter?

The app bar is one of the most-used components in all kinds of applications. It can be used to house a search field, buttons to navigate between pages, or simply the title of the page. Since it's such a commonly used component, Flutter provides a dedicated widget for this functionality called AppBar.


2 Answers

https://api.flutter.dev/flutter/widgets/PreferredSize-class.html

Any widget with a PreferredSize can appear at the bottom of an AppBar.

You can use PreferredSize to setting up your AppBar Size.

class MyApp1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Example',
        home: Scaffold(
            appBar: PreferredSize(
                preferredSize: Size.fromHeight(100.0), // here the desired height
                child: AppBar(
                  centerTitle: true,
                  title: Text("Example"),
                )
            ),

        )
    );
  }
}

enter image description here

like image 72
Amit Prajapati Avatar answered Sep 18 '22 12:09

Amit Prajapati


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.

For Ex : If you want to create a custom appbar with backgradient

import 'package:flutter/material.dart';

Color gradientStartColor = Color(0xff11998e);
Color gradientEndColor = Color(0xff0575E6);

class PreferredSizeApp extends StatefulWidget {
  @override
  _PreferredSizeAppState createState() => _PreferredSizeAppState();
}

class _PreferredSizeAppState extends State<PreferredSizeApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
          preferredSize: const Size(double.infinity, kToolbarHeight),
          child: Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(5.0)),
              boxShadow: <BoxShadow>[
                BoxShadow(
                  color: gradientStartColor,
                  offset: Offset(1.0, 6.0),
                  blurRadius: 10.0,
                ),
                BoxShadow(
                  color: gradientEndColor,
                  offset: Offset(1.0, 6.0),
                  blurRadius: 10.0,
                ),
              ],
              gradient: LinearGradient(
                  colors: [
                    gradientStartColor,
                    gradientEndColor
                  ],
                  begin: const FractionalOffset(0.2, 0.2),
                  end: const FractionalOffset(1.0, 1.0),
                  stops: [0.0, 1.0],
                  tileMode: TileMode.clamp),
            ),
            child: Center(child: Text("Appbar With Gradient",style: TextStyle(color: Colors.white,fontSize: 25.0),)),
          ),
      ),
    );
  }
}

This is the great way to use PreferredSizeWidget. I hope it helps.

like image 26
Jay Mungara Avatar answered Sep 19 '22 12:09

Jay Mungara