Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom AppBar widget?

I'm new to flutter. I'm trying to create a custom appbar widget and importing the widget in pages.

But I was unable to create the widget.

import 'package:flutter/material.dart';    class AppBar extends StatelessWidget{   @override   Widget build(BuildContext context){ return AppBar(   title: Text('Ordering'),   actions: <Widget>[     IconButton(       onPressed: _incrementCounter,       icon: Icon(Icons.add),     ),     BadgeIconButton(       itemCount: _counter,       badgeColor: Color.fromRGBO(37, 134, 16, 1.0),       badgeTextColor: Colors.white,       icon: Icon(Icons.shopping_cart, size: 30.0,),       onPressed: () {}     ),   ], ); 

} }'

like image 592
KURRU HEM Avatar asked Nov 14 '18 06:11

KURRU HEM


People also ask

Is AppBar a widget?

An app bar consists of a toolbar and potentially other widgets, such as a TabBar and a FlexibleSpaceBar. App bars typically expose one or more common actions with IconButtons which are optionally followed by a PopupMenuButton for less common operations (sometimes called the "overflow menu").

How do I add elements to my AppBar in Flutter?

You can add an icon to the AppBar by adding an IconButton widget to the actions list of the AppBar.

How can I have my AppBar in a separate file in Flutter?

if you want to make a custom appbar in a different file so that it can be shared you can just make your new app bar extend it and then pass your customization to super(), in your case your KainAppBar will extend the GradientAppBar like below how MyAppBar extends AppBar.


2 Answers

import 'package:flutter/material.dart';  class CustomAppBar extends StatefulWidget implements PreferredSizeWidget {     CustomAppBar({Key key}) : preferredSize = Size.fromHeight(kToolbarHeight), super(key: key);      @override     final Size preferredSize; // default is 56.0      @override     _CustomAppBarState createState() => _CustomAppBarState(); }  class _CustomAppBarState extends State<CustomAppBar>{      @override     Widget build(BuildContext context) {         return AppBar( title: Text("Sample App Bar") );     } } 

Hopefully this helps

like image 77
riftninja Avatar answered Sep 20 '22 12:09

riftninja


class AppBars extends AppBar {   AppBars():super(     iconTheme: IconThemeData(       color: Colors.black, //change your color here     ),     backgroundColor: Colors.white,     title: Text(       "this is app bar",       style: TextStyle(color: Color(Constant.colorBlack)),     ),     elevation: 0.0,     automaticallyImplyLeading: false,     actions: <Widget>[       IconButton(         icon: Icon(Icons.notifications),         onPressed: () => null,       ),       IconButton(         icon: Icon(Icons.person),         onPressed: () => null,       ),     ],   ); } 
like image 39
Thaiveng Avatar answered Sep 20 '22 12:09

Thaiveng