Custom Bottom AppBar in Flutter: Complete Guide

This custom bottom appbar flutter is posted by seven.srikanth at 5/2/2025 11:40:55 PM



<h1 id="custom-bottom-appbar-in-flutter-complete-guide">Custom Bottom AppBar in Flutter: Complete Guide</h1> <p>The AppBar is a fundamental component in Flutter applications, and adding a custom bottom widget can enhance its functionality and appearance. In this comprehensive guide, we'll explore various ways to customize the AppBar's bottom section.</p> <h2 id="understanding-appbar-bottom-property">Understanding AppBar Bottom Property</h2> <p>The AppBar widget's bottom property accepts a <code>PreferredSizeWidget</code>, allowing you to add custom widgets below the main AppBar content.</p> <h3 id="basic-implementation">Basic Implementation</h3> <pre>class CustomAppBar extends StatelessWidget implements PreferredSizeWidget { @override Size get preferredSize =&gt; Size.fromHeight(100); // Total AppBar height

@override Widget build(BuildContext context) { return AppBar( title: Text(&#39;Custom Bottom AppBar&#39;), bottom: PreferredSize( preferredSize: Size.fromHeight(40), // Bottom section height child: Container( height: 40, color: Colors.blue.shade800, child: Center( child: Text( &#39;Custom Bottom Section&#39;, style: TextStyle(color: Colors.white), ), ), ), ), ); } } </pre> <h2 id="custom-bottom-widgets">Custom Bottom Widgets</h2> <h3 id="search-bar-implementation">1. Search Bar Implementation</h3> <pre>class SearchAppBar extends StatelessWidget implements PreferredSizeWidget { @override Size get preferredSize =&gt; Size.fromHeight(110);

@override Widget build(BuildContext context) { return AppBar( title: Text(&#39;Search AppBar&#39;), bottom: PreferredSize( preferredSize: Size.fromHeight(50), child: Padding( padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8), child: TextField( decoration: InputDecoration( hintText: &#39;Search...&#39;, fillColor: Colors.white, filled: true, border: OutlineInputBorder( borderRadius: BorderRadius.circular(30), borderSide: BorderSide.none, ), prefixIcon: Icon(Icons.search), contentPadding: EdgeInsets.symmetric( horizontal: 20, vertical: 0, ), ), ), ), ), ); } } </pre> <h3 id="tab-bar-with-custom-design">2. Tab Bar with Custom Design</h3> <pre>class CustomTabAppBar extends StatelessWidget implements PreferredSizeWidget { @override Size get preferredSize =&gt; Size.fromHeight(100);

@override Widget build(BuildContext context) { return AppBar( title: Text(&#39;Custom Tabs&#39;), bottom: TabBar( indicator: BoxDecoration( borderRadius: BorderRadius.circular(50), color: Colors.white.withOpacity(0.2), ), labelColor: Colors.white, unselectedLabelColor: Colors.white60, tabs: [ Tab( icon: Icon(Icons.home), text: &#39;Home&#39;, ), Tab( icon: Icon(Icons.favorite), text: &#39;Favorites&#39;, ), Tab( icon: Icon(Icons.person), text: &#39;Profile&#39;, ), ], ), ); } } </pre> <h3 id="progress-indicator-bottom">3. Progress Indicator Bottom</h3> <pre>class ProgressAppBar extends StatelessWidget implements PreferredSizeWidget { final double progress;

const ProgressAppBar({ Key? key, required this.progress, }) : super(key: key);

@override Size get preferredSize =&gt; Size.fromHeight(80);

@override Widget build(BuildContext context) { return AppBar( title: Text(&#39;Progress AppBar&#39;), bottom: PreferredSize( preferredSize: Size.fromHeight(20), child: Padding( padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8), child: LinearProgressIndicator( value: progress, backgroundColor: Colors.white30, valueColor: AlwaysStoppedAnimation&lt;Color&gt;(Colors.white), ), ), ), ); } } </pre> <h2 id="advanced-implementations">Advanced Implementations</h2> <h3 id="collapsible-bottom-widget">1. Collapsible Bottom Widget</h3> <pre>class CollapsibleBottomAppBar extends StatefulWidget { @override _CollapsibleBottomAppBarState createState() =&gt; _CollapsibleBottomAppBarState(); }

class _CollapsibleBottomAppBarState extends State&lt;CollapsibleBottomAppBar&gt; { bool _isExpanded = true;

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(&#39;Collapsible Bottom&#39;), bottom: PreferredSize( preferredSize: Size.fromHeight(_isExpanded ? 50 : 0), child: AnimatedContainer( duration: Duration(milliseconds: 300), height: _isExpanded ? 50 : 0, child: SingleChildScrollView( child: Container( color: Colors.blue.shade800, padding: EdgeInsets.all(8), child: Row( children: [ Icon(Icons.info, color: Colors.white), SizedBox(width: 8), Expanded( child: Text( &#39;Additional Information&#39;, style: TextStyle(color: Colors.white), ), ), IconButton( icon: Icon( _isExpanded ? Icons.expand_less : Icons.expand_more, color: Colors.white, ), onPressed: () { setState(() ); }, ), ], ), ), ), ), ), ), body: Center( child: Text(&#39;Main Content&#39;), ), ); } } </pre> <h3 id="filter-bottom-bar">2. Filter Bottom Bar</h3> <pre>class FilterAppBar extends StatefulWidget implements PreferredSizeWidget { @override Size get preferredSize =&gt; Size.fromHeight(100);

@override _FilterAppBarState createState() =&gt; _FilterAppBarState(); }

class _FilterAppBarState extends State&lt;FilterAppBar&gt; { String _selectedFilter = &#39;All&#39;;

@override Widget build(BuildContext context) { return AppBar( title: Text(&#39;Filter AppBar&#39;), bottom: PreferredSize( preferredSize: Size.fromHeight(40), child: Container( height: 40, child: ListView( scrollDirection: Axis.horizontal, padding: EdgeInsets.symmetric(horizontal: 8), children: [ _buildFilterChip(&#39;All&#39;), _buildFilterChip(&#39;Recent&#39;), _buildFilterChip(&#39;Popular&#39;), _buildFilterChip(&#39;Trending&#39;), _buildFilterChip(&#39;Featured&#39;), ], ), ), ), ); }

Widget _buildFilterChip(String label) { return Padding( padding: EdgeInsets.symmetric(horizontal: 4), child: FilterChip( label: Text(label), selected: _selectedFilter == label, onSelected: (bool selected) { setState(() ); }, backgroundColor: Colors.white.withOpacity(0.2), selectedColor: Colors.white, labelStyle: TextStyle( color: _selectedFilter == label ? Colors.blue : Colors.white, ), ), ); } } </pre> <h2 id="best-practices">Best Practices</h2> <h3 id="responsive-height-calculation">1. Responsive Height Calculation</h3> <pre>class ResponsiveAppBar extends StatelessWidget implements PreferredSizeWidget { @override Size get preferredSize { return Size.fromHeight( kToolbarHeight + // Standard AppBar height 40.0 + // Bottom widget height MediaQuery.of(context).padding.top // Status bar height ); }

// ... rest of implementation } </pre> <h3 id="theme-integration">2. Theme Integration</h3> <pre>AppBar( bottom: PreferredSize( preferredSize: Size.fromHeight(40), child: Container( decoration: BoxDecoration( color: Theme.of(context).primaryColor.withOpacity(0.8), border: Border( bottom: BorderSide( color: Theme.of(context).dividerColor, width: 0.5, ), ), ), // ... child widgets ), ), ) </pre> <h3 id="performance-optimization">3. Performance Optimization</h3> <pre>// Use const where possible const double kBottomHeight = 40.0;

// Cache widgets that don&#39;t need rebuilding final Widget _bottomWidget = Container( height: kBottomHeight, // ... static content ); </pre> <h2 id="common-pitfalls-and-solutions">Common Pitfalls and Solutions</h2> <h3 id="height-calculation-issues">1. Height Calculation Issues</h3> <pre>// Wrong Size get preferredSize =&gt; Size.fromHeight(kToolbarHeight + 40);

// Right Size get preferredSize { final bottomHeight = bottom?.preferredSize?.height ?? 0.0; return Size.fromHeight(kToolbarHeight + bottomHeight); } </pre> <h3 id="overflow-handling">2. Overflow Handling</h3> <pre>// Prevent content overflow bottom: PreferredSize( preferredSize: Size.fromHeight(40), child: SingleChildScrollView( physics: ClampingScrollPhysics(), child: Container( // ... content ), ), ) </pre> <h2 id="conclusion">Conclusion</h2> <p>Custom bottom widgets in AppBar can significantly enhance your app's user interface and functionality. Remember to:</p> <ul> <li>Calculate heights correctly</li> <li>Handle overflow gracefully</li> <li>Consider responsiveness</li> <li>Follow platform design guidelines</li> <li>Optimize performance</li> </ul> <p>By mastering these concepts, you can create professional and user-friendly app bars that enhance your Flutter application's overall user experience.</p>


Tags: flutter,markdown,generated








0 Comments
Login to comment.
Recent Comments