Style BottomNavigationBar in Flutter

There is no option to specify the background color of BottomNavigationBar but to change the canvasColor. One way you can achieve it without messing up the whole app would be by wrapping BottomNavigationBar in a Theme with desired canvasColor. Example: bottomNavigationBar: new Theme( data: Theme.of(context).copyWith( // sets the background color of the `BottomNavigationBar` canvasColor: Colors.green, … Read more

flutter ListView KeepAlive after some scroll

For automaticKeepAlive to work, each item that needs to be kept alive must send a specific notification. A typical way to fire such notification is using AutomaticKeepAliveClientMixin class Foo extends StatefulWidget { @override FooState createState() { return new FooState(); } } class FooState extends State<Foo> with AutomaticKeepAliveClientMixin { @override Widget build(BuildContext context) { return Container( … Read more

Flutter how to draw semicircle (half circle)

Create a StatelessWidget say MyArc which accepts a diameter. import ‘dart:math’ as math; class MyArc extends StatelessWidget { final double diameter; const MyArc({super.key, this.diameter = 200}); @override Widget build(BuildContext context) { return CustomPaint( painter: MyPainter(), size: Size(diameter, diameter), ); } } // This is the Painter class class MyPainter extends CustomPainter { @override void paint(Canvas … Read more

Custom AppBar Flutter

Screenshot: Code: Using flexibleSpace Scaffold( appBar: AppBar( toolbarHeight: 120, // Set this height flexibleSpace: Container( color: Colors.orange, child: Column( children: [ Text(‘One’), Text(‘Two’), Text(‘Three’), Text(‘Four’), ], ), ), ), ) Using PreferredSize Scaffold( appBar: PreferredSize( preferredSize: Size.fromHeight(120), // Set this height child: Container( color: Colors.orange, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text(‘One’), Text(‘Two’), Text(‘Three’), Text(‘Four’), … Read more

Flutter: Keep BottomNavigationBar When Push to New Screen with Navigator

Screenshot: Starting point: void main() => runApp(MaterialApp(home: HomePage())); HomePage [BottomNavigationBar + Page1] class HomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( bottomNavigationBar: BottomNavigationBar( backgroundColor: Colors.orange, items: [ BottomNavigationBarItem(icon: Icon(Icons.call), label: ‘Call’), BottomNavigationBarItem(icon: Icon(Icons.message), label: ‘Message’), ], ), body: Navigator( onGenerateRoute: (settings) { Widget page = Page1(); if (settings.name == ‘page2’) page = … Read more

How to check if scroll position is at top or bottom in ListView?

There are generally two ways of doing it. 1. Using ScrollController // Create a variable final _controller = ScrollController(); @override void initState() { super.initState(); // Setup the listener. _controller.addListener(() { if (_controller.position.atEdge) { bool isTop = _controller.position.pixels == 0; if (isTop) { print(‘At the top’); } else { print(‘At the bottom’); } } }); } … Read more

Outlined transparent button with gradient border in flutter

I spent about two hours on it 🙂 how to use: import ‘package:flutter/material.dart’; void main() => runApp(App()); class App extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: SafeArea( child: Center( child: Column( mainAxisSize: MainAxisSize.min, children: <Widget>[ UnicornOutlineButton( strokeWidth: 2, radius: 24, gradient: LinearGradient(colors: [Colors.black, Colors.redAccent]), child: Text(‘OMG’, style: TextStyle(fontSize: 16)), … Read more

How to create GridView Layout in Flutter

Use whichever suits your need. GridView.count(…) GridView.count( crossAxisCount: 2, children: <Widget>[ FlutterLogo(), FlutterLogo(), FlutterLogo(), FlutterLogo(), ], ) GridView.builder(…) GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2), itemBuilder: (_, index) => FlutterLogo(), itemCount: 4, ) GridView(…) GridView( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2), children: <Widget>[ FlutterLogo(), FlutterLogo(), FlutterLogo(), FlutterLogo(), ], ) GridView.custom(…) GridView.custom( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2), childrenDelegate: SliverChildListDelegate( [ FlutterLogo(), FlutterLogo(), FlutterLogo(), FlutterLogo(), … Read more