mixed_viewport.dart 3.4 KB
Newer Older
1 2 3 4 5 6
// 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:math' as math;

7
import 'package:sky/widgets.dart';
8

9
class MixedViewportApp extends App {
10

11
  MixedViewportLayoutState layoutState = new MixedViewportLayoutState();
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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
  List<double> lengths = <double>[];
  double offset = 0.0;

  static const double kMaxLength = 100.0;

  static math.Random rand = new math.Random();

  void addBox() {
    lengths.add(rand.nextDouble() * kMaxLength);
    updateEnabledState();
  }

  void removeBox(int index) {
    lengths.removeAt(index);
    updateEnabledState();
  }

  void goUp() {
    offset -= 9.9;
    updateEnabledState();
  }

  void goDown() {
    offset += 20.45;
    updateEnabledState();
  }

  bool enabledDown = true;
  bool enabledUp = true;
  bool enabledAdd = true;
  bool enabledRemove = false;
  void updateEnabledState() {
    setState(() {
      enabledUp = offset > -100.0;
      enabledDown = offset < lengths.fold(0.0, (double result, double len) => result + len) + 100.0;
      enabledAdd = true;
      enabledRemove = lengths.length > 0;
    });
  }

  Widget build() {
    return new Theme(
      data: new ThemeData.light(),
      child: new Scaffold(
        toolbar: new ToolBar(
            center: new Text('Block Viewport Demo')),
        body: new Material(
          type: MaterialType.canvas,
60
          child: new Column([
61 62
              new Container(
                padding: new EdgeDims.symmetric(horizontal: 8.0, vertical: 25.0),
63
                child: new Row([
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
                    new RaisedButton(
                      enabled: enabledAdd,
                      child: new Text('ADD'),
                      onPressed: addBox
                    ),
                    new RaisedButton(
                      enabled: enabledUp,
                      child: new Text('UP'),
                      onPressed: goUp
                    ),
                    new RaisedButton(
                      enabled: enabledDown,
                      child: new Text('DOWN'),
                      onPressed: goDown
                    )
                  ],
                  justifyContent: FlexJustifyContent.spaceAround
                )
              ),
              new Flexible(
                child: new Container(
                  margin: new EdgeDims.all(8.0),
                  decoration: new BoxDecoration(
87
                    border: new Border.all(color: new Color(0xFF000000))
88 89
                  ),
                  padding: new EdgeDims.all(16.0),
90
                  child: new MixedViewport(
91 92
                    builder: builder,
                    startOffset: offset,
93 94
                    token: lengths.length,
                    layoutState: layoutState
95 96 97 98 99 100 101 102 103 104 105 106 107 108
                  )
                )
              ),
            ],
            justifyContent: FlexJustifyContent.spaceBetween
          )
        )
      )
    );
  }

  Widget builder(int index) {
    if (index >= lengths.length)
      return null;
109
    return new GestureDetector(
110
      key: new ValueKey<double>(lengths[index]),
111
      onTap: () => removeBox(index),
112 113 114 115 116
      child: new Container(
        decoration: new BoxDecoration(
          backgroundColor: new Color((0xFF000000 + 0xFFFFFF * lengths[index] / kMaxLength).round())
        ),
        height: lengths[index] + 12.0
117
      )
118 119 120 121 122 123
    );
  }

}

void main() {
124
  runApp(new MixedViewportApp());
125
}