Create this class.
class CustomAppBar extends StatelessWidget implements PreferredSizeWidget {final Widget child;final double height;CustomAppBar({required this.child,this.height = kToolbarHeight,});@overrideSize get preferredSize => Size.fromHeight(height);@overrideWidget build(BuildContext context) {return Container(height: preferredSize.height,color: Colors.red,child: child,);}}
Usage:
Scaffold(appBar: CustomAppBar(height: 100,child: Column(children: [FlutterLogo(size: 56),SizedBox(height: 8),Text('Flutter'),],),),)
Edit to riftninja's answer :
import 'package:flutter/material.dart';class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {CustomAppBar({Key key, double height}) : preferredSize = Size.fromHeight(height), super(key: key);@override//final Size preferredSize; // This didnot work for me.Size get preferredSize => preferredSize; //This should work.@override_CustomAppBarState createState() => _CustomAppBarState();}class _CustomAppBarState extends State<CustomAppBar>{@overrideWidget build(BuildContext context) {return AppBar( title: Text("Sample App Bar") );}}
This also works for stateless widget.
widget_appbar.dart
class WidgetAppBar extends StatelessWidget implements PreferredSizeWidget {final Color? backgroundColor;final Color? textIconColor;final String? icon;final String? title;final double? height;final List<Widget>? menuItem;final bool hideBack;WidgetAppBar({this.backgroundColor = whiteColor,this.textIconColor = headingColor,this.icon,this.title = '',this.menuItem,this.height: kToolbarHeight,this.hideBack = false,});@overrideSize get preferredSize => Size.fromHeight(height!);@overrideWidget build(BuildContext context) {return AppBar(actions: menuItem,toolbarHeight: preferredSize.height,iconTheme: IconThemeData(color: textIconColor,),leading: hideBack? Container(): icon == null? BackButton(): IconButton(icon: Image.asset(icon!,height: 18,width: 18,),onPressed: () {Navigator.pop(context, true);},),title: Text(title!,style: TextStyle(fontSize: 20,fontWeight: FontWeight.bold,color: textIconColor,),),backgroundColor: backgroundColor,centerTitle: true,);}}
How to use?
@overrideWidget build(BuildContext context) {return Scaffold(appBar: WidgetAppBar(icon: ic_back_black,title: 'Toolbar Title',),body: SafeArea(child: Container(),),);}