flutter_logo.dart 18.8 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;
import 'dart:typed_data';
7
import 'dart:ui' as ui show Gradient, TextBox, lerpDouble;
8

9
import 'package:flutter/foundation.dart';
10

11
import 'alignment.dart';
12
import 'basic_types.dart';
13
import 'box_fit.dart';
14
import 'decoration.dart';
15
import 'edge_insets.dart';
16
import 'image_provider.dart';
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import 'text_painter.dart';
import 'text_span.dart';
import 'text_style.dart';

/// Possible ways to draw Flutter's logo.
enum FlutterLogoStyle {
  /// Show only Flutter's logo, not the "Flutter" label.
  ///
  /// This is the default behavior for [FlutterLogoDecoration] objects.
  markOnly,

  /// Show Flutter's logo on the left, and the "Flutter" label to its right.
  horizontal,

  /// Show Flutter's logo above the "Flutter" label.
  stacked,
}

/// An immutable description of how to paint Flutter's logo.
class FlutterLogoDecoration extends Decoration {
  /// Creates a decoration that knows how to paint Flutter's logo.
  ///
39 40 41
  /// The [lightColor] and [darkColor] are used to fill the logo. The [style]
  /// controls whether and where to draw the "Flutter" label. If one is shown,
  /// the [textColor] controls the color of the label.
42
  ///
43 44
  /// The [lightColor], [darkColor], [textColor], [style], and [margin]
  /// arguments must not be null.
45
  const FlutterLogoDecoration({
46 47
    this.lightColor: const Color(0xFF42A5F5), // Colors.blue[400]
    this.darkColor: const Color(0xFF0D47A1), // Colors.blue[900]
48
    this.textColor: const Color(0xFF616161),
49
    this.style: FlutterLogoStyle.markOnly,
50
    this.margin: EdgeInsets.zero,
51 52 53 54 55 56
  }) : assert(lightColor != null),
       assert(darkColor != null),
       assert(textColor != null),
       assert(style != null),
       assert(margin != null),
       _position = style == FlutterLogoStyle.markOnly ? 0.0 : style == FlutterLogoStyle.horizontal ? 1.0 : -1.0, // ignore: CONST_EVAL_TYPE_BOOL_NUM_STRING
57 58 59
       // (see https://github.com/dart-lang/sdk/issues/26980 for details about that ignore statement)
       _opacity = 1.0;

60
  const FlutterLogoDecoration._(this.lightColor, this.darkColor, this.textColor, this.style, this.margin, this._position, this._opacity);
61

62
  /// The lighter of the two colors used to paint the logo.
63
  ///
64
  /// If possible, the default should be used. It corresponds to the 400 and 900
65
  /// values of [material.Colors.blue] from the Material library.
66
  ///
67
  /// If for some reason that color scheme is impractical, the same entries from
68 69 70
  /// [material.Colors.amber], [material.Colors.red], or
  /// [material.Colors.indigo] colors can be used. These are Flutter's secondary
  /// colors.
71 72
  ///
  /// In extreme cases where none of those four color schemes will work,
73 74
  /// [material.Colors.pink], [material.Colors.purple], or
  /// [material.Colors.cyan] can be used. These are Flutter's tertiary colors.
75 76 77 78 79 80
  final Color lightColor;

  /// The darker of the two colors used to paint the logo.
  ///
  /// See [lightColor] for more information about selecting the logo's colors.
  final Color darkColor;
81

82 83 84 85 86 87
  /// The color used to paint the "Flutter" text on the logo, if [style] is
  /// [FlutterLogoStyle.horizontal] or [FlutterLogoStyle.stacked]. The
  /// appropriate color is `const Color(0xFF616161)` (a medium gray), against a
  /// white background.
  final Color textColor;

88 89 90 91 92 93
  /// Whether and where to draw the "Flutter" text. By default, only the logo
  /// itself is drawn.
  // This property isn't actually used when painting. It's only really used to
  // set the internal _position property.
  final FlutterLogoStyle style;

94 95 96
  /// How far to inset the logo from the edge of the container.
  final EdgeInsets margin;

97 98 99 100 101 102 103 104
  // The following are set when lerping, to represent states that can't be
  // represented by the constructor.
  final double _position; // -1.0 for stacked, 1.0 for horizontal, 0.0 for no logo
  final double _opacity; // 0.0 .. 1.0

  bool get _inTransition => _opacity != 1.0 || (_position != -1.0 && _position != 0.0 && _position != 1.0);

  @override
105
  bool debugAssertIsValid() {
106 107
    assert(lightColor != null
        && darkColor != null
108
        && textColor != null
109
        && style != null
110
        && margin != null
111 112 113 114
        && _position != null
        && _position.isFinite
        && _opacity != null
        && _opacity >= 0.0
115
        && _opacity <= 1.0);
116 117 118 119 120 121 122 123 124 125
    return true;
  }

  @override
  bool get isComplex => !_inTransition;

  /// Linearly interpolate between two Flutter logo descriptions.
  ///
  /// Interpolates both the color and the style in a continuous fashion.
  ///
126 127
  /// If both values are null, this returns null. Otherwise, it returns a
  /// non-null value. If one of the values is null, then the result is obtained
128
  /// by scaling the other value's opacity and [margin].
129
  ///
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
  /// The `t` argument represents position on the timeline, with 0.0 meaning
  /// that the interpolation has not started, returning `a` (or something
  /// equivalent to `a`), 1.0 meaning that the interpolation has finished,
  /// returning `b` (or something equivalent to `b`), and values in between
  /// meaning that the interpolation is at the relevant point on the timeline
  /// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
  /// 1.0, so negative values and values greater than 1.0 are valid (and can
  /// easily be generated by curves such as [Curves.elasticInOut]).
  ///
  /// Values for `t` are usually obtained from an [Animation<double>], such as
  /// an [AnimationController].
  ///
  /// See also:
  ///
  ///  * [Decoration.lerp], which interpolates between arbitrary decorations.
145
  static FlutterLogoDecoration lerp(FlutterLogoDecoration a, FlutterLogoDecoration b, double t) {
146
    assert(t != null);
147 148
    assert(a == null || a.debugAssertIsValid());
    assert(b == null || b.debugAssertIsValid());
149 150 151 152
    if (a == null && b == null)
      return null;
    if (a == null) {
      return new FlutterLogoDecoration._(
153 154
        b.lightColor,
        b.darkColor,
155
        b.textColor,
156
        b.style,
157
        b.margin * t,
158 159 160 161 162 163
        b._position,
        b._opacity * t.clamp(0.0, 1.0),
      );
    }
    if (b == null) {
      return new FlutterLogoDecoration._(
164 165
        a.lightColor,
        a.darkColor,
166
        a.textColor,
167
        a.style,
168
        a.margin * t,
169 170 171 172
        a._position,
        a._opacity * (1.0 - t).clamp(0.0, 1.0),
      );
    }
173 174 175 176
    if (t == 0.0)
      return a;
    if (t == 1.0)
      return b;
177
    return new FlutterLogoDecoration._(
178 179
      Color.lerp(a.lightColor, b.lightColor, t),
      Color.lerp(a.darkColor, b.darkColor, t),
180
      Color.lerp(a.textColor, b.textColor, t),
181
      t < 0.5 ? a.style : b.style,
182
      EdgeInsets.lerp(a.margin, b.margin, t),
183 184 185 186 187 188 189
      a._position + (b._position - a._position) * t,
      (a._opacity + (b._opacity - a._opacity) * t).clamp(0.0, 1.0),
    );
  }

  @override
  FlutterLogoDecoration lerpFrom(Decoration a, double t) {
190
    assert(debugAssertIsValid());
191 192 193 194 195
    if (a == null || a is FlutterLogoDecoration) {
      assert(a == null || a.debugAssertIsValid());
      return FlutterLogoDecoration.lerp(a, this, t);
    }
    return super.lerpFrom(a, t);
196 197 198 199
  }

  @override
  FlutterLogoDecoration lerpTo(Decoration b, double t) {
200
    assert(debugAssertIsValid());
201 202 203 204 205
    if (b == null || b is FlutterLogoDecoration) {
      assert(b == null || b.debugAssertIsValid());
      return FlutterLogoDecoration.lerp(this, b, t);
    }
    return super.lerpTo(b, t);
206 207 208 209
  }

  @override
  // TODO(ianh): better hit testing
210
  bool hitTest(Size size, Offset position, { TextDirection textDirection }) => true;
211 212 213

  @override
  BoxPainter createBoxPainter([VoidCallback onChanged]) {
214
    assert(debugAssertIsValid());
215 216 217 218 219
    return new _FlutterLogoPainter(this);
  }

  @override
  bool operator ==(dynamic other) {
220
    assert(debugAssertIsValid());
221 222 223 224 225
    if (identical(this, other))
      return true;
    if (other is! FlutterLogoDecoration)
      return false;
    final FlutterLogoDecoration typedOther = other;
226 227
    return lightColor == typedOther.lightColor
        && darkColor == typedOther.darkColor
228
        && textColor == typedOther.textColor
229 230 231 232 233 234
        && _position == typedOther._position
        && _opacity == typedOther._opacity;
  }

  @override
  int get hashCode {
235
    assert(debugAssertIsValid());
236
    return hashValues(
237 238
      lightColor,
      darkColor,
239
      textColor,
240 241 242 243 244 245
      _position,
      _opacity
    );
  }

  @override
246 247 248 249 250 251
  void debugFillProperties(DiagnosticPropertiesBuilder properties) {
    super.debugFillProperties(properties);
    properties.add(new DiagnosticsNode.message('$lightColor/$darkColor on $textColor'));
    properties.add(new EnumProperty<FlutterLogoStyle>('style', style));
    if (_inTransition)
      properties.add(new DiagnosticsNode.message('transition $_position:$_opacity'));
252 253 254 255 256 257
  }
}


/// An object that paints a [BoxDecoration] into a canvas.
class _FlutterLogoPainter extends BoxPainter {
258 259 260 261
  _FlutterLogoPainter(this._config)
    : assert(_config != null),
      assert(_config.debugAssertIsValid()),
      super(null) {
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
    _prepareText();
  }

  final FlutterLogoDecoration _config;

  // these are configured assuming a font size of 100.0.
  TextPainter _textPainter;
  Rect _textBoundingRect;

  void _prepareText() {
    const String kLabel = 'Flutter';
    _textPainter = new TextPainter(
      text: new TextSpan(
        text: kLabel,
        style: new TextStyle(
277
          color: _config.textColor,
278 279 280
          fontFamily: 'Roboto',
          fontSize: 100.0 * 350.0 / 247.0, // 247 is the height of the F when the fontSize is 350, assuming device pixel ratio 1.0
          fontWeight: FontWeight.w300,
Ian Hickson's avatar
Ian Hickson committed
281 282 283 284
          textBaseline: TextBaseline.alphabetic,
        ),
      ),
      textDirection: TextDirection.ltr,
285 286
    );
    _textPainter.layout();
287
    final ui.TextBox textSize = _textPainter.getBoxesForSelection(const TextSelection(baseOffset: 0, extentOffset: kLabel.length)).single;
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    _textBoundingRect = new Rect.fromLTRB(textSize.left, textSize.top, textSize.right, textSize.bottom);
  }

  // This class contains a lot of magic numbers. They were derived from the
  // values in the SVG files exported from the original artwork source.

  void _paintLogo(Canvas canvas, Rect rect) {
    // Our points are in a coordinate space that's 166 pixels wide and 202 pixels high.
    // First, transform the rectangle so that our coordinate space is a square 202 pixels
    // to a side, with the top left at the origin.
    canvas.save();
    canvas.translate(rect.left, rect.top);
    canvas.scale(rect.width / 202.0, rect.height / 202.0);
    // Next, offset it some more so that the 166 horizontal pixels are centered
    // in that square (as opposed to being on the left side of it). This means
    // that if we draw in the rectangle from 0,0 to 166,202, we are drawing in
    // the center of the given rect.
    canvas.translate((202.0 - 166.0) / 2.0, 0.0);

    // Set up the styles.
    final Paint lightPaint = new Paint()
309
      ..color = _config.lightColor.withOpacity(0.8);
310
    final Paint mediumPaint = new Paint()
311
      ..color = _config.lightColor;
312
    final Paint darkPaint = new Paint()
313
      ..color = _config.darkColor;
314 315

    final ui.Gradient triangleGradient = new ui.Gradient.linear(
316 317
      const Offset(87.2623 + 37.9092, 28.8384 + 123.4389),
      const Offset(42.9205 + 37.9092, 35.0952 + 123.4389),
318 319 320 321 322 323 324 325 326 327 328
      <Color>[
        const Color(0xBFFFFFFF),
        const Color(0xBFFCFCFC),
        const Color(0xBFF4F4F4),
        const Color(0xBFE5E5E5),
        const Color(0xBFD1D1D1),
        const Color(0xBFB6B6B6),
        const Color(0xBF959595),
        const Color(0xBF6E6E6E),
        const Color(0xBF616161),
      ],
329
      <double>[ 0.2690, 0.4093, 0.4972, 0.5708, 0.6364, 0.6968, 0.7533, 0.8058, 0.8219 ],
330 331 332
    );
    final Paint trianglePaint = new Paint()
      ..shader = triangleGradient
333
      ..blendMode = BlendMode.multiply;
334 335

    final ui.Gradient rectangleGradient = new ui.Gradient.linear(
336 337
      const Offset(62.3643 + 37.9092, 40.135 + 123.4389),
      const Offset(54.0376 + 37.9092, 31.8083 + 123.4389),
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
      <Color>[
        const Color(0x80FFFFFF),
        const Color(0x80FCFCFC),
        const Color(0x80F4F4F4),
        const Color(0x80E5E5E5),
        const Color(0x80D1D1D1),
        const Color(0x80B6B6B6),
        const Color(0x80959595),
        const Color(0x806E6E6E),
        const Color(0x80616161),
      ],
      <double>[ 0.4588, 0.5509, 0.6087, 0.6570, 0.7001, 0.7397, 0.7768, 0.8113, 0.8219 ],
    );
    final Paint rectanglePaint = new Paint()
      ..shader = rectangleGradient
353
      ..blendMode = BlendMode.multiply;
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

    // Draw the basic shape.
    final Path topBeam = new Path()
      ..moveTo(37.7, 128.9)
      ..lineTo(9.8, 101.0)
      ..lineTo(100.4, 10.4)
      ..lineTo(156.2, 10.4);
    canvas.drawPath(topBeam, lightPaint);

    final Path middleBeam = new Path()
      ..moveTo(156.2, 94.0)
      ..lineTo(100.4, 94.0)
      ..lineTo(79.5, 114.9)
      ..lineTo(107.4, 142.8);
    canvas.drawPath(middleBeam, lightPaint);

    final Path bottomBeam = new Path()
      ..moveTo(79.5, 170.7)
      ..lineTo(100.4, 191.6)
      ..lineTo(156.2, 191.6)
      ..lineTo(156.2, 191.6)
      ..lineTo(107.4, 142.8);
    canvas.drawPath(bottomBeam, darkPaint);

    canvas.save();
    canvas.transform(new Float64List.fromList(const <double>[
      // careful, this is in _column_-major order
      0.7071, -0.7071, 0.0, 0.0,
      0.7071, 0.7071, 0.0, 0.0,
      0.0, 0.0, 1.0, 0.0,
      -77.697, 98.057, 0.0, 1.0,
    ]));
    canvas.drawRect(new Rect.fromLTWH(59.8, 123.1, 39.4, 39.4), mediumPaint);
    canvas.restore();

    // The two gradients.
    final Path triangle = new Path()
      ..moveTo(79.5, 170.7)
      ..lineTo(120.9, 156.4)
      ..lineTo(107.4, 142.8);
    canvas.drawPath(triangle, trianglePaint);

    final Path rectangle = new Path()
      ..moveTo(107.4, 142.8)
      ..lineTo(79.5, 170.7)
      ..lineTo(86.1, 177.3)
      ..lineTo(114.0, 149.4);
    canvas.drawPath(rectangle, rectanglePaint);

    canvas.restore();
  }

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    offset += _config.margin.topLeft;
409
    final Size canvasSize = _config.margin.deflateSize(configuration.size);
410 411
    if (canvasSize.isEmpty)
      return;
412 413 414 415 416 417 418 419 420 421 422
    Size logoSize;
    if (_config._position > 0.0) {
      // horizontal style
      logoSize = const Size(820.0, 232.0);
    } else if (_config._position < 0.0) {
      // stacked style
      logoSize = const Size(252.0, 306.0);
    } else {
      // only the mark
      logoSize = const Size(202.0, 202.0);
    }
423
    final FittedSizes fittedSize = applyBoxFit(BoxFit.contain, logoSize, canvasSize);
424
    assert(fittedSize.source == logoSize);
425
    final Rect rect = Alignment.center.inscribe(fittedSize.destination, offset & canvasSize);
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
    final double centerSquareHeight = canvasSize.shortestSide;
    final Rect centerSquare = new Rect.fromLTWH(
      offset.dx + (canvasSize.width - centerSquareHeight) / 2.0,
      offset.dy + (canvasSize.height - centerSquareHeight) / 2.0,
      centerSquareHeight,
      centerSquareHeight
    );

    Rect logoTargetSquare;
    if (_config._position > 0.0) {
      // horizontal style
      logoTargetSquare = new Rect.fromLTWH(rect.left, rect.top, rect.height, rect.height);
    } else if (_config._position < 0.0) {
      // stacked style
      final double logoHeight = rect.height * 191.0 / 306.0;
      logoTargetSquare = new Rect.fromLTWH(
        rect.left + (rect.width - logoHeight) / 2.0,
        rect.top,
        logoHeight,
        logoHeight
      );
    } else {
      // only the mark
      logoTargetSquare = centerSquare;
    }
    final Rect logoSquare = Rect.lerp(centerSquare, logoTargetSquare, _config._position.abs());

    if (_config._opacity < 1.0) {
      canvas.saveLayer(
        offset & canvasSize,
        new Paint()
          ..colorFilter = new ColorFilter.mode(
            const Color(0xFFFFFFFF).withOpacity(_config._opacity),
459
            BlendMode.modulate,
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
          )
      );
    }
    if (_config._position != 0.0) {
      if (_config._position > 0.0) {
        // horizontal style
        final double fontSize = 2.0 / 3.0 * logoSquare.height * (1 - (10.4 * 2.0) / 202.0);
        final double scale = fontSize / 100.0;
        final double finalLeftTextPosition = // position of text in rest position
          (256.4 / 820.0) * rect.width - // 256.4 is the distance from the left edge to the left of the F when the whole logo is 820.0 wide
          (32.0 / 350.0) * fontSize; // 32 is the distance from the text bounding box edge to the left edge of the F when the font size is 350
        final double initialLeftTextPosition = // position of text when just starting the animation
          rect.width / 2.0 - _textBoundingRect.width * scale;
        final Offset textOffset = new Offset(
          rect.left + ui.lerpDouble(initialLeftTextPosition, finalLeftTextPosition, _config._position),
          rect.top + (rect.height - _textBoundingRect.height * scale) / 2.0
        );
        canvas.save();
        if (_config._position < 1.0) {
479
          final Offset center = logoSquare.center;
480
          final Path path = new Path()
481 482 483
            ..moveTo(center.dx, center.dy)
            ..lineTo(center.dx + rect.width, center.dy - rect.width)
            ..lineTo(center.dx + rect.width, center.dy + rect.width)
484 485 486 487 488 489 490 491 492 493 494 495
            ..close();
          canvas.clipPath(path);
        }
        canvas.translate(textOffset.dx, textOffset.dy);
        canvas.scale(scale, scale);
        _textPainter.paint(canvas, Offset.zero);
        canvas.restore();
      } else if (_config._position < 0.0) {
        // stacked style
        final double fontSize = 0.35 * logoTargetSquare.height * (1 - (10.4 * 2.0) / 202.0);
        final double scale = fontSize / 100.0;
        if (_config._position > -1.0) {
496
          // This limits what the drawRect call below is going to blend with.
497 498 499 500 501
          canvas.saveLayer(_textBoundingRect, new Paint());
        } else {
          canvas.save();
        }
        canvas.translate(
502
          logoTargetSquare.center.dx - (_textBoundingRect.width * scale / 2.0),
503 504 505 506 507 508
          logoTargetSquare.bottom
        );
        canvas.scale(scale, scale);
        _textPainter.paint(canvas, Offset.zero);
        if (_config._position > -1.0) {
          canvas.drawRect(_textBoundingRect.inflate(_textBoundingRect.width * 0.5), new Paint()
509
            ..blendMode = BlendMode.modulate
510
            ..shader = new ui.Gradient.linear(
511 512
              new Offset(_textBoundingRect.width * -0.5, 0.0),
              new Offset(_textBoundingRect.width * 1.5, 0.0),
513 514 515 516 517 518 519 520 521 522 523 524 525
              <Color>[const Color(0xFFFFFFFF), const Color(0xFFFFFFFF), const Color(0x00FFFFFF), const Color(0x00FFFFFF)],
              <double>[ 0.0, math.max(0.0, _config._position.abs() - 0.1), math.min(_config._position.abs() + 0.1, 1.0), 1.0 ],
            )
          );
        }
        canvas.restore();
      }
    }
    _paintLogo(canvas, logoSquare);
    if (_config._opacity < 1.0)
      canvas.restore();
  }
}