In this blog, we will explore about Expandable floating Action Button in Flutter.
The floating action button is an important widget in the Application. Users can Explore multiple features through it.
The floating Action Button can be used to show extra activities from your project. But a few action buttons in one single may hassle your main app screen. In that case, we can use an expandable floating button which may help us to make our application’s UI more eye-catchy.
So, without wasting time let’s start the code implementation of Expandable Floating Action Button(FAB) in Flutter.
You may also check our Flutter App Development Company page.
#MainFunction
Just add the following code in the main. dart file of your project.
Here I have created the action buttons which I want to show with the click of the Floating Action Button(FAB).
You can create the number of Action Buttons according to your need and requirement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
floatingActionButton: ExpandableFabClass( distanceBetween: 80.0, subChildren: [ ActionButton( onPressed: () => print('ABC'), icon: const Icon(Icons.share), ), ActionButton( onPressed: () => print('DEF'), icon: const Icon(Icons.favorite), ), ], ), |
#ExpandableFabClass
Now, you need to create a new Expandable FAB as given below.
Here, I have set the Expandable FAB design, timing, distance, animation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
import 'dart:math' as math; import 'package:flutter/material.dart'; @immutable class ExpandableFabClass extends StatefulWidget { const ExpandableFabClass({ Key? key, this.isInitiallyOpen, required this.distanceBetween, required this.subChildren, }) : super(key: key); final bool? isInitiallyOpen; final double distanceBetween; final List<Widget> subChildren; @override _ExpandableFabClassState createState() => _ExpandableFabClassState(); } class _ExpandableFabClassState extends State<ExpandableFabClass> with SingleTickerProviderStateMixin { late final AnimationController _animationController; late final Animation<double> _expandAnimationFab; bool _open = false; @override void initState() { super.initState(); _open = widget.isInitiallyOpen ?? false; _animationController = AnimationController( value: _open ? 1.0 : 0.0, duration: const Duration(milliseconds: 250), vsync: this, ); _expandAnimationFab = CurvedAnimation( curve: Curves.fastOutSlowIn, reverseCurve: Curves.easeOutQuad, parent: _animationController, ); } @override void dispose() { _animationController.dispose(); super.dispose(); } void _toggle() { setState(() { _open = !_open; if (_open) { _animationController.forward(); } else { _animationController.reverse(); } }); } @override Widget build(BuildContext context) { return SizedBox.expand( child: Stack( alignment: Alignment.bottomRight, clipBehavior: Clip.none, children: [ _buildTapToCloseFab(), ..._buildExpandingActionButtons(), _buildTapToOpenFab(), ], ), ); } Widget _buildTapToCloseFab() { return SizedBox( width: 56.0, height: 56.0, child: Center( child: Material( shape: const CircleBorder(), clipBehavior: Clip.antiAlias, elevation: 4.0, child: InkWell( onTap: _toggle, child: Padding( padding: const EdgeInsets.all(8.0), child: Icon( Icons.close, color: Theme.of(context).primaryColor, ), ), ), ), ), ); } List<Widget> _buildExpandingActionButtons() { final children = <Widget>[]; final count = widget.subChildren.length; final step = 90.0 / (count - 1); for (var i = 0, angleInDegrees = 0.0; i < count; i++, angleInDegrees += step) { children.add( _ExpandingActionButton( directionInDegrees: angleInDegrees, maxDistance: widget.distanceBetween, progress: _expandAnimationFab, child: widget.subChildren[i], ), ); } return children; } Widget _buildTapToOpenFab() { return IgnorePointer( ignoring: _open, child: AnimatedContainer( transformAlignment: Alignment.center, transform: Matrix4.diagonal3Values( _open ? 0.7 : 1.0, _open ? 0.7 : 1.0, 1.0, ), duration: const Duration(milliseconds: 250), curve: const Interval(0.0, 0.5, curve: Curves.easeOut), child: AnimatedOpacity( opacity: _open ? 0.0 : 1.0, curve: const Interval(0.25, 1.0, curve: Curves.easeInOut), duration: const Duration(milliseconds: 250), child: FloatingActionButton( onPressed: _toggle, child: const Icon(Icons.create), ), ), ), ); } } @immutable class _ExpandingActionButton extends StatelessWidget { _ExpandingActionButton({ Key? key, required this.directionInDegrees, required this.maxDistance, required this.progress, required this.child, }) : super(key: key); final double directionInDegrees; final double maxDistance; final Animation<double> progress; final Widget child; @override Widget build(BuildContext context) { return AnimatedBuilder( animation: progress, builder: (context, child) { final offset = Offset.fromDirection( directionInDegrees * (math.pi / 180.0), progress.value * maxDistance, ); return Positioned( right: 4.0 + offset.dx, bottom: 4.0 + offset.dy, child: Transform.rotate( angle: (1.0 - progress.value) * math.pi / 2, child: child!, ), ); }, child: FadeTransition( opacity: progress, child: child, ), ); } } |
Just copy the above code and paste it into your expandable FAB class.
#Output
The code implementation part has been done. Now we will see the output in the below-attached video.
Congratulations!!!! you have learned how to implement the Expandable Floating Action Button in Flutter.
For more details and methods you can refer to the official doc of flutter here.
For more interesting blogs check out here – https://mobikul.com/blog/
Hope this blog helped you with a better understanding of the implementation of Expandable FAB in flutter.
Thanks for reading.😇