interactive_flex.dart 3.28 KB
Newer Older
1 2 3 4 5 6 7
// 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.

import 'dart:sky';
import 'dart:math' as math;

8
import 'package:sky/mojo/activity.dart';
9
import 'package:sky/mojo/net/image_cache.dart' as image_cache;
10
import 'package:sky/rendering.dart';
11 12 13 14 15 16 17 18 19 20 21 22

import 'solid_color_box.dart';

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

class RenderImageGrow extends RenderImage {
  final Size _startingSize;

23 24
  RenderImageGrow(Image image, Size size)
    : _startingSize = size, super(image: image, width: size.width, height: size.height);
25 26 27 28 29

  double _growth = 0.0;
  double get growth => _growth;
  void set growth(double value) {
    _growth = value;
30 31
    width = _startingSize.width == null ? null : _startingSize.width + growth;
    height = _startingSize.height == null ? null : _startingSize.height + growth;
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  }
}

RenderImageGrow image;

Map<int, Touch> touches = new Map();
void handleEvent(event) {
  if (event is PointerEvent) {
    if (event.type == 'pointermove')
      image.growth = math.max(0.0, image.growth + event.x - touches[event.pointer].x);
    touches[event.pointer] = new Touch(event.x, event.y);
  }
  if (event.type == "back") {
    activity.finishCurrentActivity();
  }
}

void main() {
  void addFlexChildSolidColor(RenderFlex parent, Color backgroundColor, { int flex: 0 }) {
    RenderSolidColorBox child = new RenderSolidColorBox(backgroundColor);
    parent.add(child);
    child.parentData.flex = flex;
  }

  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));
Eric Seidel's avatar
Eric Seidel committed
63
  image_cache.load("https://www.dartlang.org/logos/dart-logo.png").first.then((Image dartLogo) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    image.image = dartLogo;
  });

  var padding = new RenderPadding(padding: const EdgeDims.all(10.0), child: image);
  row.add(padding);

  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.""";
Eric Seidel's avatar
Eric Seidel committed
82
  var text = new StyledTextSpan(
83
      new TextStyle(color:  const Color(0xFF009900)),
Eric Seidel's avatar
Eric Seidel committed
84
      [new PlainTextSpan(meatyString)]);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  padding = new RenderPadding(
      padding: const EdgeDims.all(10.0),
      child: new RenderParagraph(text));
  column.add(padding);

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

  row.add(column);
  column.parentData.flex = 8;

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

101
  updateTaskDescription('Interactive Flex', topColor);
102 103 104
  new SkyBinding(root: root);
  view.setEventCallback(handleEvent);
}