sector.dart 5.39 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 8
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
9

Hixie's avatar
Hixie committed
10
import 'package:flutter_rendering_examples/sector_layout.dart';
11 12 13 14 15 16 17 18

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

Hixie's avatar
Hixie committed
19 20 21 22 23
class SectorApp extends StatefulComponent {
  SectorAppState createState() => new SectorAppState();
}

class SectorAppState extends State<SectorApp> {
24

Hixie's avatar
Hixie committed
25 26
  final RenderBoxToRenderSectorAdapter sectors = initCircle();
  final math.Random rand = new math.Random(1);
27

28 29 30 31
  List<double> wantedSectorSizes = <double>[];
  List<double> actualSectorSizes = <double>[];
  double get currentTheta => wantedSectorSizes.fold(0.0, (double total, double value) => total + value);

32
  void addSector() {
33 34 35 36 37 38 39 40 41 42
    final double currentTheta = this.currentTheta;
    if (currentTheta < kTwoPi) {
      double deltaTheta;
      if (currentTheta >= kTwoPi - (math.PI * 0.2 + 0.05))
        deltaTheta = kTwoPi - currentTheta;
      else
        deltaTheta = math.PI * rand.nextDouble() / 5.0 + 0.05;
      wantedSectorSizes.add(deltaTheta);
      updateEnabledState();
    }
43 44 45
  }

  void removeSector() {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    if (wantedSectorSizes.isNotEmpty) {
      wantedSectorSizes.removeLast();
      updateEnabledState();
    }
  }

  void doUpdates() {
    int index = 0;
    while (index < actualSectorSizes.length && index < wantedSectorSizes.length && actualSectorSizes[index] == wantedSectorSizes[index])
      index += 1;
    RenderSectorRing ring = sectors.child;
    while (index < actualSectorSizes.length) {
      ring.remove(ring.lastChild);
      actualSectorSizes.removeLast();
    }
    while (index < wantedSectorSizes.length) {
      Color color = new Color(((0xFF << 24) + rand.nextInt(0xFFFFFF)) | 0x808080);
      ring.add(new RenderSolidColor(color, desiredDeltaTheta: wantedSectorSizes[index]));
      actualSectorSizes.add(wantedSectorSizes[index]);
      index += 1;
    }
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  }

  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));

Hixie's avatar
Hixie committed
82 83
  bool _enabledAdd = true;
  bool _enabledRemove = false;
84 85
  void updateEnabledState() {
    setState(() {
86 87
      _enabledAdd = currentTheta < kTwoPi;
      _enabledRemove = wantedSectorSizes.isNotEmpty;
88 89 90 91
    });
  }

  Widget buildBody() {
92 93
    return new Column(
      children: <Widget>[
94 95
        new Container(
          padding: new EdgeDims.symmetric(horizontal: 8.0, vertical: 25.0),
96 97
          child: new Row(
            children: <Widget>[
98
              new RaisedButton(
99
                onPressed: _enabledAdd ? addSector : null,
100
                child: new IntrinsicWidth(
101 102 103 104 105
                  child: new Row(
                    children: <Widget>[
                      new Container(
                        padding: new EdgeDims.all(4.0),
                        margin: new EdgeDims.only(right: 10.0),
106
                        child: new WidgetToRenderBoxAdapter(renderBox: sectorAddIcon)
107 108 109 110
                      ),
                      new Text('ADD SECTOR'),
                    ]
                  )
111
                )
112
              ),
113
              new RaisedButton(
114
                onPressed: _enabledRemove ? removeSector : null,
115
                child: new IntrinsicWidth(
116 117 118 119 120
                  child: new Row(
                    children: <Widget>[
                      new Container(
                        padding: new EdgeDims.all(4.0),
                        margin: new EdgeDims.only(right: 10.0),
121
                        child: new WidgetToRenderBoxAdapter(renderBox: sectorRemoveIcon)
122 123 124 125
                      ),
                      new Text('REMOVE SECTOR'),
                    ]
                  )
126 127
                )
              ),
128 129 130 131 132 133 134 135 136 137 138
            ],
            justifyContent: FlexJustifyContent.spaceAround
          )
        ),
        new Flexible(
          child: new Container(
            margin: new EdgeDims.all(8.0),
            decoration: new BoxDecoration(
              border: new Border.all(color: new Color(0xFF000000))
            ),
            padding: new EdgeDims.all(8.0),
139 140 141 142
            child: new WidgetToRenderBoxAdapter(
              renderBox: sectors,
              onBuild: doUpdates
            )
143 144 145 146
          )
        ),
      ],
      justifyContent: FlexJustifyContent.spaceBetween
147 148 149
    );
  }

Hixie's avatar
Hixie committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: new ThemeData.light(),
      title: 'Sector Layout',
      routes: <String, RouteBuilder>{
        '/': (RouteArguments args) {
          return new Scaffold(
            toolBar: new ToolBar(
              center: new Text('Sector Layout in a Widget Tree')
            ),
            body: buildBody()
          );
        }
      }
164 165 166 167 168 169 170
    );
  }
}

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