sector.dart 4.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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/rendering/box.dart';
import 'package:sky/rendering/flex.dart';
import 'package:sky/widgets/basic.dart';
import 'package:sky/widgets/material.dart';
import 'package:sky/widgets/raised_button.dart';
import 'package:sky/widgets/scaffold.dart';
import 'package:sky/widgets/task_description.dart';
import 'package:sky/widgets/theme.dart';
import 'package:sky/widgets/tool_bar.dart';
16
import 'package:sky/widgets/framework.dart';
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 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

import '../rendering/sector_layout.dart';

RenderBox initCircle() {
  return new RenderBoxToRenderSectorAdapter(
    innerRadius: 25.0,
    child: new RenderSectorRing(padding: 0.0)
  );
}

class SectorApp extends App {

  RenderBoxToRenderSectorAdapter sectors = initCircle();
  math.Random rand = new math.Random(1);

  void addSector() {
    double deltaTheta;
    var ring = (sectors.child as RenderSectorRing);
    SectorDimensions currentSize = ring.getIntrinsicDimensions(const SectorConstraints(), ring.deltaRadius);
    if (currentSize.deltaTheta >= kTwoPi - (math.PI * 0.2 + 0.05))
      deltaTheta = kTwoPi - currentSize.deltaTheta;
    else
      deltaTheta = math.PI * rand.nextDouble() / 5.0 + 0.05;
    Color color = new Color(((0xFF << 24) + rand.nextInt(0xFFFFFF)) | 0x808080);
    ring.add(new RenderSolidColor(color, desiredDeltaTheta: deltaTheta));
    updateEnabledState();
  }

  void removeSector() {
    (sectors.child as RenderSectorRing).remove((sectors.child as RenderSectorRing).lastChild);
    updateEnabledState();
  }

  static RenderBox initSector(Color color) {
    RenderSectorRing ring = new RenderSectorRing(padding: 1.0);
    ring.add(new RenderSolidColor(const Color(0xFF909090), desiredDeltaTheta: kTwoPi * 0.15));
    ring.add(new RenderSolidColor(const Color(0xFF909090), desiredDeltaTheta: kTwoPi * 0.15));
    ring.add(new RenderSolidColor(color, desiredDeltaTheta: kTwoPi * 0.2));
    return new RenderBoxToRenderSectorAdapter(
      innerRadius: 5.0,
      child: ring
    );
  }
  RenderBoxToRenderSectorAdapter sectorAddIcon = initSector(const Color(0xFF00DD00));
  RenderBoxToRenderSectorAdapter sectorRemoveIcon = initSector(const Color(0xFFDD0000));

  bool enabledAdd = true;
  bool enabledRemove = false;
  void updateEnabledState() {
    setState(() {
      var ring = (sectors.child as RenderSectorRing);
      SectorDimensions currentSize = ring.getIntrinsicDimensions(const SectorConstraints(), ring.deltaRadius);
      enabledAdd = currentSize.deltaTheta < kTwoPi;
      enabledRemove = ring.firstChild != null;
    });
  }

  Widget buildBody() {
    return 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 ShrinkWrapWidth(
                    child: new Flex([
                      new Container(
                        padding: new EdgeDims.all(4.0),
                        margin: new EdgeDims.only(right: 10.0),
                        child: new WidgetToRenderBoxAdapter(sectorAddIcon)
                      ),
                      new Text('ADD SECTOR'),
                    ])
                  ),
                  onPressed: addSector
                ),
                new RaisedButton(
                  enabled: enabledRemove,
                  child: new ShrinkWrapWidth(
                    child: new Flex([
                      new Container(
                        padding: new EdgeDims.all(4.0),
                        margin: new EdgeDims.only(right: 10.0),
                        child: new WidgetToRenderBoxAdapter(sectorRemoveIcon)
                      ),
                      new Text('REMOVE SECTOR'),
                    ])
                  ),
                  onPressed: removeSector
                )
              ],
              justifyContent: FlexJustifyContent.spaceAround
            )
          ),
          new Flexible(
            child: new Container(
              margin: new EdgeDims.all(8.0),
              decoration: new BoxDecoration(
117
                border: new Border.all(color: new Color(0xFF000000))
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
              ),
              padding: new EdgeDims.all(8.0),
              child: new WidgetToRenderBoxAdapter(sectors)
            )
          ),
        ],
        direction: FlexDirection.vertical,
        justifyContent: FlexJustifyContent.spaceBetween
      )
    );
  }

  Widget build() {
    return new Theme(
      data: new ThemeData.light(),
      child: new TaskDescription(
        label: 'Sector Layout',
        child: new Scaffold(
          toolbar: new ToolBar(
            center: new Text('Sector Layout in a Widget Tree')
          ),
          body: buildBody()
        )
      )
    );
  }
}

void main() {
  runApp(new SectorApp());
}