Created
July 6, 2020 08:44
-
-
Save Parassharmaa/fd13c76564acbf079d10891ffcb98ee0 to your computer and use it in GitHub Desktop.
Notched Bottom Bar Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:math' as math; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: MyWidget(), | |
), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
backgroundColor: Colors.black54, | |
bottomNavigationBar: new BottomAppBar( | |
shape: SlantNotch(), | |
notchMargin: 12, | |
color: Colors.white, | |
child: new Row( | |
mainAxisSize: MainAxisSize.max, | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: <Widget>[ | |
IconButton( | |
icon: Icon(Icons.home), | |
onPressed: () {}, | |
), | |
IconButton( | |
icon: Icon(Icons.explore), | |
onPressed: () {}, | |
), | |
IconButton( | |
icon: Icon(Icons.search), | |
onPressed: () {}, | |
), | |
IconButton( | |
icon: Icon(Icons.shop), | |
onPressed: () {}, | |
), | |
], | |
), | |
), | |
floatingActionButton: new FloatingActionButton( | |
backgroundColor: Colors.red, | |
onPressed: () {}, | |
child: Icon(Icons.add, color: Colors.white), | |
shape: CircleBorder( | |
side: BorderSide(color: Colors.white, width: 5), | |
), | |
), | |
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, | |
); | |
} | |
} | |
class SlantNotch extends NotchedShape { | |
@override | |
Path getOuterPath(Rect host, Rect guest) { | |
if (guest == null || !host.overlaps(guest)) return Path()..addRect(host); | |
// The guest's shape is a circle bounded by the guest rectangle. | |
// So the guest's radius is half the guest width. | |
final double notchRadius = guest.width / 2.0; | |
// We build a path for the notch from 3 segments: | |
// Segment A - a Bezier curve from the host's top edge to segment B. | |
// Segment B - an arc with radius notchRadius. | |
// Segment C - a Bezier curve from segment B back to the host's top edge. | |
// | |
// A detailed explanation and the derivation of the formulas below is | |
// available at: https://goo.gl/Ufzrqn | |
const double s1 = 15.0; | |
const double s2 = 1.0; | |
final double r = notchRadius; | |
final double a = -1.0 * r - s2; | |
final double b = host.top - guest.center.dy; | |
final double n2 = math.sqrt(b * b * r * r * (a * a + b * b - r * r)); | |
final double p2xA = ((a * r * r) - n2) / (a * a + b * b); | |
final double p2xB = ((a * r * r) + n2) / (a * a + b * b); | |
final double p2yA = math.sqrt(r * r - p2xA * p2xA); | |
final double p2yB = math.sqrt(r * r - p2xB * p2xB); | |
final List<Offset> p = List<Offset>(6); | |
// p0, p1, and p2 are the control points for segment A. | |
p[0] = Offset(a - s1, b); | |
p[1] = Offset(a, b); | |
final double cmp = b < 0 ? -1.0 : 1.0; | |
p[2] = cmp * p2yA > cmp * p2yB ? Offset(p2xA, p2yA) : Offset(p2xB, p2yB); | |
// p3, p4, and p5 are the control points for segment B, which is a mirror | |
// of segment A around the y axis. | |
p[3] = Offset(-1.0 * p[2].dx, p[2].dy); | |
p[4] = Offset(-1.0 * p[1].dx, p[1].dy); | |
p[5] = Offset(-1.0 * p[0].dx, p[0].dy); | |
// translate all points back to the absolute coordinate system. | |
for (int i = 0; i < p.length; i += 1) p[i] += guest.center; | |
double slantHeight = r / 2.5; | |
return Path() | |
..moveTo(host.left, host.top) | |
..lineTo(p[0].dx, p[0].dy - slantHeight) | |
..quadraticBezierTo(p[1].dx, p[1].dy - slantHeight, p[2].dx, p[2].dy) | |
..arcToPoint( | |
p[3], | |
radius: Radius.circular(notchRadius), | |
clockwise: false, | |
) | |
..quadraticBezierTo(p[4].dx, p[4].dy - slantHeight, p[5].dx, p[5].dy - slantHeight) | |
..lineTo(host.right, host.top) | |
..lineTo(host.right, host.bottom) | |
..lineTo(host.left, host.bottom) | |
..close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment