block_viewport.dart 3.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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;

import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/block_viewport.dart';
import 'package:sky/widgets/material.dart';
import 'package:sky/widgets/raised_button.dart';
import 'package:sky/widgets/scaffold.dart';
import 'package:sky/widgets/theme.dart';
import 'package:sky/widgets/tool_bar.dart';
14
import 'package:sky/widgets/framework.dart';
15 16 17

class BlockViewportApp extends App {

18
  BlockViewportLayoutState layoutState = new BlockViewportLayoutState();
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  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,
          child: new Flex([
              new Container(
                padding: new EdgeDims.symmetric(horizontal: 8.0, vertical: 25.0),
                child: new Flex([
                    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(
94
                    border: new Border.all(color: new Color(0xFF000000))
95 96 97 98 99
                  ),
                  padding: new EdgeDims.all(16.0),
                  child: new BlockViewport(
                    builder: builder,
                    startOffset: offset,
100 101
                    token: lengths.length,
                    layoutState: layoutState
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
                  )
                )
              ),
            ],
            direction: FlexDirection.vertical,
            justifyContent: FlexJustifyContent.spaceBetween
          )
        )
      )
    );
  }

  Widget builder(int index) {
    if (index >= lengths.length)
      return null;
    return new Listener(
118
      key: new Key.stringify(lengths[index]),
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      child: new Container(
        decoration: new BoxDecoration(
          backgroundColor: new Color((0xFF000000 + 0xFFFFFF * lengths[index] / kMaxLength).round())
        ),
        height: lengths[index] + 12.0
      ),
      onGestureTap: (_) {
        removeBox(index);
      }
    );
  }

}

void main() {
  runApp(new BlockViewportApp());
  // scheduler.addPersistentFrameCallback((_) {
  //   SkyBinding.instance.debugDumpRenderTree();
  // });
}