"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/packages/flutter/lib/src/painting/notched_shapes.dart" (13 Nov 2020, 6240 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 
    6 import 'dart:math' as math;
    7 
    8 import 'basic_types.dart';
    9 import 'borders.dart';
   10 
   11 /// A shape with a notch in its outline.
   12 ///
   13 /// Typically used as the outline of a 'host' widget to make a notch that
   14 /// accommodates a 'guest' widget. e.g the [BottomAppBar] may have a notch to
   15 /// accommodate the [FloatingActionButton].
   16 ///
   17 /// See also:
   18 ///
   19 ///  * [ShapeBorder], which defines a shaped border without a dynamic notch.
   20 ///  * [AutomaticNotchedShape], an adapter from [ShapeBorder] to [NotchedShape].
   21 abstract class NotchedShape {
   22   /// Abstract const constructor. This constructor enables subclasses to provide
   23   /// const constructors so that they can be used in const expressions.
   24   const NotchedShape();
   25 
   26   /// Creates a [Path] that describes the outline of the shape.
   27   ///
   28   /// The `host` is the bounding rectangle of the shape.
   29   ///
   30   /// The `guest` is the bounding rectangle of the shape for which a notch will
   31   /// be made. It is null when there is no guest.
   32   Path getOuterPath(Rect host, Rect? guest);
   33 }
   34 
   35 /// A rectangle with a smooth circular notch.
   36 ///
   37 /// See also:
   38 ///
   39 ///  * [CircleBorder], a [ShapeBorder] that describes a circle.
   40 class CircularNotchedRectangle extends NotchedShape {
   41   /// Creates a [CircularNotchedRectangle].
   42   ///
   43   /// The same object can be used to create multiple shapes.
   44   const CircularNotchedRectangle();
   45 
   46   /// Creates a [Path] that describes a rectangle with a smooth circular notch.
   47   ///
   48   /// `host` is the bounding box for the returned shape. Conceptually this is
   49   /// the rectangle to which the notch will be applied.
   50   ///
   51   /// `guest` is the bounding box of a circle that the notch accommodates. All
   52   /// points in the circle bounded by `guest` will be outside of the returned
   53   /// path.
   54   ///
   55   /// The notch is curve that smoothly connects the host's top edge and
   56   /// the guest circle.
   57   // TODO(amirh): add an example diagram here.
   58   @override
   59   Path getOuterPath(Rect host, Rect? guest) {
   60     if (guest == null || !host.overlaps(guest))
   61       return Path()..addRect(host);
   62 
   63     // The guest's shape is a circle bounded by the guest rectangle.
   64     // So the guest's radius is half the guest width.
   65     final double notchRadius = guest.width / 2.0;
   66 
   67     // We build a path for the notch from 3 segments:
   68     // Segment A - a Bezier curve from the host's top edge to segment B.
   69     // Segment B - an arc with radius notchRadius.
   70     // Segment C - a Bezier curve from segment B back to the host's top edge.
   71     //
   72     // A detailed explanation and the derivation of the formulas below is
   73     // available at: https://goo.gl/Ufzrqn
   74 
   75     const double s1 = 15.0;
   76     const double s2 = 1.0;
   77 
   78     final double r = notchRadius;
   79     final double a = -1.0 * r - s2;
   80     final double b = host.top - guest.center.dy;
   81 
   82     final double n2 = math.sqrt(b * b * r * r * (a * a + b * b - r * r));
   83     final double p2xA = ((a * r * r) - n2) / (a * a + b * b);
   84     final double p2xB = ((a * r * r) + n2) / (a * a + b * b);
   85     final double p2yA = math.sqrt(r * r - p2xA * p2xA);
   86     final double p2yB = math.sqrt(r * r - p2xB * p2xB);
   87 
   88     final List<Offset?> p = List<Offset?>.filled(6, null, growable: false);
   89 
   90     // p0, p1, and p2 are the control points for segment A.
   91     p[0] = Offset(a - s1, b);
   92     p[1] = Offset(a, b);
   93     final double cmp = b < 0 ? -1.0 : 1.0;
   94     p[2] = cmp * p2yA > cmp * p2yB ? Offset(p2xA, p2yA) : Offset(p2xB, p2yB);
   95 
   96     // p3, p4, and p5 are the control points for segment B, which is a mirror
   97     // of segment A around the y axis.
   98     p[3] = Offset(-1.0 * p[2]!.dx, p[2]!.dy);
   99     p[4] = Offset(-1.0 * p[1]!.dx, p[1]!.dy);
  100     p[5] = Offset(-1.0 * p[0]!.dx, p[0]!.dy);
  101 
  102     // translate all points back to the absolute coordinate system.
  103     for (int i = 0; i < p.length; i += 1)
  104       p[i] = p[i]! + guest.center;
  105 
  106     return Path()
  107       ..moveTo(host.left, host.top)
  108       ..lineTo(p[0]!.dx, p[0]!.dy)
  109       ..quadraticBezierTo(p[1]!.dx, p[1]!.dy, p[2]!.dx, p[2]!.dy)
  110       ..arcToPoint(
  111         p[3]!,
  112         radius: Radius.circular(notchRadius),
  113         clockwise: false,
  114       )
  115       ..quadraticBezierTo(p[4]!.dx, p[4]!.dy, p[5]!.dx, p[5]!.dy)
  116       ..lineTo(host.right, host.top)
  117       ..lineTo(host.right, host.bottom)
  118       ..lineTo(host.left, host.bottom)
  119       ..close();
  120   }
  121 }
  122 
  123 /// A [NotchedShape] created from [ShapeBorder]s.
  124 ///
  125 /// Two shapes can be provided. The [host] is the shape of the widget that
  126 /// uses the [NotchedShape] (typically a [BottomAppBar]). The [guest] is
  127 /// subtracted from the [host] to create the notch (typically to make room
  128 /// for a [FloatingActionButton]).
  129 class AutomaticNotchedShape extends NotchedShape {
  130   /// Creates a [NotchedShape] that is defined by two [ShapeBorder]s.
  131   ///
  132   /// The [host] must not be null.
  133   ///
  134   /// The [guest] may be null, in which case no notch is created even
  135   /// if a guest rectangle is provided to [getOuterPath].
  136   const AutomaticNotchedShape(this.host, [ this.guest ]);
  137 
  138   /// The shape of the widget that uses the [NotchedShape] (typically a
  139   /// [BottomAppBar]).
  140   ///
  141   /// This shape cannot depend on the [TextDirection], as no text direction
  142   /// is available to [NotchedShape]s.
  143   final ShapeBorder host;
  144 
  145   /// The shape to subtract from the [host] to make the notch.
  146   ///
  147   /// This shape cannot depend on the [TextDirection], as no text direction
  148   /// is available to [NotchedShape]s.
  149   ///
  150   /// If this is null, [getOuterPath] ignores the guest rectangle.
  151   final ShapeBorder? guest;
  152 
  153   @override
  154   Path getOuterPath(Rect hostRect, Rect? guestRect) { // ignore: avoid_renaming_method_parameters, the
  155     // parameters are renamed over the baseclass because they would clash
  156     // with properties of this object, and the use of all four of them in
  157     // the code below is really confusing if they have the same names.
  158     final Path hostPath = host.getOuterPath(hostRect);
  159     if (guest != null && guestRect != null) {
  160       final Path guestPath = guest!.getOuterPath(guestRect);
  161       return Path.combine(PathOperation.difference, hostPath, guestPath);
  162     }
  163     return hostPath;
  164   }
  165 }