From c810b0501ff30322f02fe1379195ff0f7dcf49ac Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 30 Nov 2015 14:21:38 -0800 Subject: [PATCH] PopupMenu shouldn't use CustomPaint Align is now powerful enough to do all the work we need to do here. --- .../flutter/lib/src/material/popup_menu.dart | 75 ++++++------------- .../flutter/lib/src/rendering/proxy_box.dart | 30 ++++++-- 2 files changed, 43 insertions(+), 62 deletions(-) diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 105fd392e1..5fca1b41b0 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -5,13 +5,11 @@ import 'dart:async'; import 'package:flutter/animation.dart'; -import 'package:flutter/painting.dart'; import 'package:flutter/widgets.dart'; import 'ink_well.dart'; +import 'material.dart'; import 'popup_menu_item.dart'; -import 'shadows.dart'; -import 'theme.dart'; const Duration _kMenuDuration = const Duration(milliseconds: 300); const double _kMenuCloseIntervalEnd = 2.0 / 3.0; @@ -21,38 +19,6 @@ const double _kMenuMaxWidth = 5.0 * _kMenuWidthStep; const double _kMenuHorizontalPadding = 16.0; const double _kMenuVerticalPadding = 8.0; -class _PopupMenuPainter extends CustomPainter { - const _PopupMenuPainter({ - this.color, - this.elevation, - this.width, - this.height - }); - - final Color color; - final int elevation; - final double width; - final double height; - - void paint(Canvas canvas, Size size) { - double widthValue = width * size.width; - double heightValue = height * size.height; - final BoxPainter painter = new BoxPainter(new BoxDecoration( - backgroundColor: color, - borderRadius: 2.0, - boxShadow: elevationToShadow[elevation] - )); - painter.paint(canvas, new Rect.fromLTWH(size.width - widthValue, 0.0, widthValue, heightValue)); - } - - bool shouldRepaint(_PopupMenuPainter oldPainter) { - return oldPainter.color != color - || oldPainter.elevation != elevation - || oldPainter.width != width - || oldPainter.height != height; - } -} - class _PopupMenu extends StatelessComponent { _PopupMenu({ Key key, @@ -88,25 +54,26 @@ class _PopupMenu extends StatelessComponent { builder: (BuildContext context) { return new Opacity( opacity: opacity.value, - child: new CustomPaint( - painter: new _PopupMenuPainter( - color: Theme.of(context).canvasColor, - elevation: route.elevation, - width: width.value, - height: height.value - ), - child: new ConstrainedBox( - constraints: new BoxConstraints( - minWidth: _kMenuMinWidth, - maxWidth: _kMenuMaxWidth - ), - child: new IntrinsicWidth( - stepWidth: _kMenuWidthStep, - child: new Block( - children, - padding: const EdgeDims.symmetric( - horizontal: _kMenuHorizontalPadding, - vertical: _kMenuVerticalPadding + child: new Material( + type: MaterialType.card, + elevation: route.elevation, + child: new Align( + alignment: const FractionalOffset(1.0, 0.0), + widthFactor: width.value, + heightFactor: height.value, + child: new ConstrainedBox( + constraints: new BoxConstraints( + minWidth: _kMenuMinWidth, + maxWidth: _kMenuMaxWidth + ), + child: new IntrinsicWidth( + stepWidth: _kMenuWidthStep, + child: new Block( + children, + padding: const EdgeDims.symmetric( + horizontal: _kMenuHorizontalPadding, + vertical: _kMenuVerticalPadding + ) ) ) ) diff --git a/packages/flutter/lib/src/rendering/proxy_box.dart b/packages/flutter/lib/src/rendering/proxy_box.dart index 78c3ca468b..3150913803 100644 --- a/packages/flutter/lib/src/rendering/proxy_box.dart +++ b/packages/flutter/lib/src/rendering/proxy_box.dart @@ -162,8 +162,8 @@ class RenderFractionallySizedBox extends RenderProxyBox { double widthFactor, double heightFactor }) : _widthFactor = widthFactor, _heightFactor = heightFactor, super(child) { - assert(_widthFactor == null || _widthFactor > 0.0); - assert(_heightFactor == null || _heightFactor > 0.0); + assert(_widthFactor == null || _widthFactor >= 0.0); + assert(_heightFactor == null || _heightFactor >= 0.0); } /// The multiple to apply to the incoming maximum width constraint to use as @@ -172,7 +172,7 @@ class RenderFractionallySizedBox extends RenderProxyBox { double get widthFactor => _widthFactor; double _widthFactor; void set widthFactor (double value) { - assert(value == null || value > 0.0); + assert(value == null || value >= 0.0); if (_widthFactor == value) return; _widthFactor = value; @@ -185,7 +185,7 @@ class RenderFractionallySizedBox extends RenderProxyBox { double get heightFactor => _heightFactor; double _heightFactor; void set heightFactor (double value) { - assert(value == null || value > 0.0); + assert(value == null || value >= 0.0); if (_heightFactor == value) return; _heightFactor = value; @@ -193,11 +193,25 @@ class RenderFractionallySizedBox extends RenderProxyBox { } BoxConstraints _getInnerConstraints(BoxConstraints constraints) { + double minWidth = constraints.minWidth; + double maxWidth = constraints.maxWidth; + if (_widthFactor != null) { + double width = maxWidth * _widthFactor; + minWidth = width; + maxWidth = width; + } + double minHeight = constraints.minHeight; + double maxHeight = constraints.maxHeight; + if (_heightFactor != null) { + double height = maxHeight * _heightFactor; + minHeight = height; + maxHeight = height; + } return new BoxConstraints( - minWidth: _widthFactor == null ? constraints.minWidth : constraints.maxWidth * _widthFactor, - maxWidth: _widthFactor == null ? constraints.maxWidth : constraints.maxWidth * _widthFactor, - minHeight: _heightFactor == null ? constraints.minHeight : constraints.maxHeight * _heightFactor, - maxHeight: _heightFactor == null ? constraints.maxHeight : constraints.maxHeight * _heightFactor + minWidth: minWidth, + maxWidth: maxWidth, + minHeight: minHeight, + maxHeight: maxHeight ); }