interactive_flex.dart 4.01 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
import 'dart:ui' as ui;
6
import 'dart:math' as math;
7
import 'dart:typed_data';
8

Ian Hickson's avatar
Ian Hickson committed
9
import 'package:flutter/scheduler.dart';
10 11
import 'package:flutter/services.dart';
import 'package:flutter/rendering.dart';
12 13 14
import 'package:mojo/bindings.dart' as bindings;
import 'package:mojo/core.dart' as core;
import 'package:sky_services/pointer/pointer.mojom.dart';
15

16
import 'lib/solid_color_box.dart';
17 18 19 20 21 22 23 24 25 26

class Touch {
  final double x;
  final double y;
  const Touch(this.x, this.y);
}

class RenderImageGrow extends RenderImage {
  final Size _startingSize;

27
  RenderImageGrow(ui.Image image, Size size)
28
    : _startingSize = size, super(image: image, width: size.width, height: size.height);
29 30 31 32 33

  double _growth = 0.0;
  double get growth => _growth;
  void set growth(double value) {
    _growth = value;
34 35
    width = _startingSize.width == null ? null : _startingSize.width + growth;
    height = _startingSize.height == null ? null : _startingSize.height + growth;
36 37 38 39 40
  }
}

RenderImageGrow image;

Ian Hickson's avatar
Ian Hickson committed
41 42 43 44 45 46
class DemoBinding extends BindingBase with Scheduler, Renderer {
  DemoBinding({ RenderBox root }) {
    renderView.child = root;
    ui.window.onPopRoute = handlePopRoute;
    ui.window.onPointerPacket = handlePointerPacket;
  }
47

Ian Hickson's avatar
Ian Hickson committed
48 49 50
  void handlePopRoute() {
    activity.finishCurrentActivity();
  }
51

Ian Hickson's avatar
Ian Hickson committed
52 53 54 55 56 57 58 59 60 61 62
  final Map<int, Touch> touches = <int, Touch>{};

  void handlePointerPacket(ByteData serializedPacket) {
    bindings.Message message = new bindings.Message(
      serializedPacket,
      <core.MojoHandle>[],
      serializedPacket.lengthInBytes,
      0
    );
    PointerPacket packet = PointerPacket.deserialize(message);
    for (Pointer pointer in packet.pointers) {
63
      if (pointer.type == PointerType.move)
Ian Hickson's avatar
Ian Hickson committed
64 65 66
        image.growth = math.max(0.0, image.growth + pointer.x - touches[pointer.pointer].x);
      touches[pointer.pointer] = new Touch(pointer.x, pointer.y);
    }
67 68 69 70 71 72 73
  }
}

void main() {
  void addFlexChildSolidColor(RenderFlex parent, Color backgroundColor, { int flex: 0 }) {
    RenderSolidColorBox child = new RenderSolidColorBox(backgroundColor);
    parent.add(child);
74 75
    final FlexParentData childParentData = child.parentData;
    childParentData.flex = flex;
76 77 78 79 80 81 82 83 84
  }

  var row = new RenderFlex(direction: FlexDirection.horizontal);

  // Left cell
  addFlexChildSolidColor(row, const Color(0xFF00D2B8), flex: 1);

  // Resizeable image
  image = new RenderImageGrow(null, new Size(100.0, null));
Ian Hickson's avatar
Ian Hickson committed
85
  imageCache.load("http://flutter.io/favicon.ico").first.then((ui.Image dartLogo) {
86 87 88
    image.image = dartLogo;
  });

Hixie's avatar
Hixie committed
89
  row.add(new RenderPadding(padding: const EdgeDims.all(10.0), child: image));
90 91 92 93 94 95 96 97 98 99 100 101 102

  RenderFlex column = new RenderFlex(direction: FlexDirection.vertical);

  // Top cell
  final Color topColor = const Color(0xFF55DDCA);
  addFlexChildSolidColor(column, topColor, flex: 1);

  // The internet is a beautiful place.  https://baconipsum.com/
  String meatyString = """Bacon ipsum dolor amet ham fatback tri-tip, prosciutto
porchetta bacon kevin meatball meatloaf pig beef ribs chicken. Brisket ribeye
andouille leberkas capicola meatloaf. Chicken pig ball tip pork picanha bresaola
alcatra. Pork pork belly alcatra, flank chuck drumstick biltong doner jowl.
Pancetta meatball tongue tenderloin rump tail jowl boudin.""";
Hixie's avatar
Hixie committed
103 104 105 106 107 108 109 110
  TextSpan text = new StyledTextSpan(
    new TextStyle(color:  const Color(0xFF009900)),
    <TextSpan>[new PlainTextSpan(meatyString)]
  );
  column.add(new RenderPadding(
    padding: const EdgeDims.all(10.0),
    child: new RenderParagraph(text)
  ));
111 112 113 114 115

  // Bottom cell
  addFlexChildSolidColor(column, const Color(0xFF0081C6), flex: 2);

  row.add(column);
116 117
  final FlexParentData childParentData = column.parentData;
  childParentData.flex = 8;
118 119 120 121 122 123

  RenderDecoratedBox root = new RenderDecoratedBox(
    decoration: new BoxDecoration(backgroundColor: const Color(0xFFFFFFFF)),
    child: row
  );

Ian Hickson's avatar
Ian Hickson committed
124
  updateTaskDescription(label: 'Interactive Flex', color: topColor);
Ian Hickson's avatar
Ian Hickson committed
125
  new DemoBinding(root: root);
126
}