Move the notch computation from the FAB to the BAB. (#18372)
Move the notch computation from the FAB to the BAB. The notch in the BAB (bottom action bar) for the FAB (floating action button) was previously kept as part of the FAB's implementation. This was done to keep the shape of the FAB and the shape of the notch coupled. That approach resulted in a somewhat complex and 'non Fluttery' mechanism for propagating the notch computation from the FAB to the BAB. This CL uncouples the FAB and the notch computation. With the new API the BAB computes its overall shape including the notch using a NotchedShape delegate. This includes multiple breaking changes: * Scaffold.setFloatingActionButtonNotchFor is deleted. * The ComputeNotch type is deleted. * The hasNotch property of BottomAppBar is deleted. * The notchMargin property of FloatingActionButton is deleted. Quick migration guide from the previous API: | Previous API | New API | | ------------------|-------------| | BottomAppBar(hasNotch: false) | BottomAppBar() | | Using a FloatingActionButton with: BottomAppBar() / BottomAppBar(hasNotch: true) | BottomAppBar(shape: CircularNotchedRectangle()) | | Scaffold.setFloatingActionButtonNotchFor(..) | No longer supported |
This commit is contained in:
@@ -187,10 +187,20 @@ class _BottomAppBarDemoState extends State<BottomAppBarDemo> {
|
||||
bottomNavigationBar: new _DemoBottomAppBar(
|
||||
color: _babColor,
|
||||
fabLocation: _fabLocation.value,
|
||||
showNotch: _showNotch.value,
|
||||
shape: _selectNotch(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
NotchedShape _selectNotch() {
|
||||
if (!_showNotch.value)
|
||||
return null;
|
||||
if (_fabShape == kCircularFab)
|
||||
return const CircularNotchedRectangle();
|
||||
if (_fabShape == kDiamondFab)
|
||||
return const _DiamondNotchedRectangle();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class _ChoiceValue<T> {
|
||||
@@ -317,11 +327,15 @@ class _Heading extends StatelessWidget {
|
||||
}
|
||||
|
||||
class _DemoBottomAppBar extends StatelessWidget {
|
||||
const _DemoBottomAppBar({ this.color, this.fabLocation, this.showNotch });
|
||||
const _DemoBottomAppBar({
|
||||
this.color,
|
||||
this.fabLocation,
|
||||
this.shape
|
||||
});
|
||||
|
||||
final Color color;
|
||||
final FloatingActionButtonLocation fabLocation;
|
||||
final bool showNotch;
|
||||
final NotchedShape shape;
|
||||
|
||||
static final List<FloatingActionButtonLocation> kCenterLocations = <FloatingActionButtonLocation>[
|
||||
FloatingActionButtonLocation.centerDocked,
|
||||
@@ -369,8 +383,8 @@ class _DemoBottomAppBar extends StatelessWidget {
|
||||
|
||||
return new BottomAppBar(
|
||||
color: color,
|
||||
hasNotch: showNotch,
|
||||
child: new Row(children: rowContents),
|
||||
shape: shape,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -399,64 +413,46 @@ class _DemoDrawer extends StatelessWidget {
|
||||
}
|
||||
|
||||
// A diamond-shaped floating action button.
|
||||
class _DiamondFab extends StatefulWidget {
|
||||
class _DiamondFab extends StatelessWidget {
|
||||
const _DiamondFab({
|
||||
this.child,
|
||||
this.notchMargin = 6.0,
|
||||
this.onPressed,
|
||||
});
|
||||
|
||||
final Widget child;
|
||||
final double notchMargin;
|
||||
final VoidCallback onPressed;
|
||||
|
||||
@override
|
||||
State createState() => new _DiamondFabState();
|
||||
}
|
||||
|
||||
class _DiamondFabState extends State<_DiamondFab> {
|
||||
|
||||
VoidCallback _clearComputeNotch;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return new Material(
|
||||
shape: const _DiamondBorder(),
|
||||
color: Colors.orange,
|
||||
child: new InkWell(
|
||||
onTap: widget.onPressed,
|
||||
onTap: onPressed,
|
||||
child: new Container(
|
||||
width: 56.0,
|
||||
height: 56.0,
|
||||
child: IconTheme.merge(
|
||||
data: new IconThemeData(color: Theme.of(context).accentIconTheme.color),
|
||||
child: widget.child,
|
||||
child: child,
|
||||
),
|
||||
),
|
||||
),
|
||||
elevation: 6.0,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _DiamondNotchedRectangle implements NotchedShape {
|
||||
const _DiamondNotchedRectangle();
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_clearComputeNotch = Scaffold.setFloatingActionButtonNotchFor(context, _computeNotch);
|
||||
}
|
||||
Path getOuterPath(Rect host, Rect guest) {
|
||||
if (!host.overlaps(guest))
|
||||
return new Path()..addRect(host);
|
||||
assert(guest.width > 0.0);
|
||||
|
||||
@override
|
||||
void deactivate() {
|
||||
if (_clearComputeNotch != null)
|
||||
_clearComputeNotch();
|
||||
super.deactivate();
|
||||
}
|
||||
|
||||
Path _computeNotch(Rect host, Rect guest, Offset start, Offset end) {
|
||||
final Rect marginedGuest = guest.inflate(widget.notchMargin);
|
||||
if (!host.overlaps(marginedGuest))
|
||||
return new Path()..lineTo(end.dx, end.dy);
|
||||
|
||||
final Rect intersection = marginedGuest.intersect(host);
|
||||
final Rect intersection = guest.intersect(host);
|
||||
// We are computing a "V" shaped notch, as in this diagram:
|
||||
// -----\**** /-----
|
||||
// \ /
|
||||
@@ -470,14 +466,18 @@ class _DiamondFabState extends State<_DiamondFab> {
|
||||
// the host's top edge where the notch starts (marked with "*").
|
||||
// We compute notchToCenter by similar triangles:
|
||||
final double notchToCenter =
|
||||
intersection.height * (marginedGuest.height / 2.0)
|
||||
/ (marginedGuest.width / 2.0);
|
||||
intersection.height * (guest.height / 2.0)
|
||||
/ (guest.width / 2.0);
|
||||
|
||||
return new Path()
|
||||
..lineTo(marginedGuest.center.dx - notchToCenter, host.top)
|
||||
..lineTo(marginedGuest.left + marginedGuest.width / 2.0, marginedGuest.bottom)
|
||||
..lineTo(marginedGuest.center.dx + notchToCenter, host.top)
|
||||
..lineTo(end.dx, end.dy);
|
||||
..moveTo(host.left, host.top)
|
||||
..lineTo(guest.center.dx - notchToCenter, host.top)
|
||||
..lineTo(guest.left + guest.width / 2.0, guest.bottom)
|
||||
..lineTo(guest.center.dx + notchToCenter, host.top)
|
||||
..lineTo(host.right, host.top)
|
||||
..lineTo(host.right, host.bottom)
|
||||
..lineTo(host.left, host.bottom)
|
||||
..close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user