sectors.dart 5.27 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// 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:flutter/material.dart';
8

9
import '../rendering/src/sector_layout.dart';
10

11
RenderBoxToRenderSectorAdapter initCircle() {
12
  return RenderBoxToRenderSectorAdapter(
13
    innerRadius: 25.0,
14
    child: RenderSectorRing(padding: 0.0),
15 16 17
  );
}

18
class SectorApp extends StatefulWidget {
19 20
  const SectorApp({Key? key}) : super(key: key);

21
  @override
22
  SectorAppState createState() => SectorAppState();
Hixie's avatar
Hixie committed
23 24 25
}

class SectorAppState extends State<SectorApp> {
26

Hixie's avatar
Hixie committed
27
  final RenderBoxToRenderSectorAdapter sectors = initCircle();
28
  final math.Random rand = math.Random(1);
29

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

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

  void removeSector() {
48 49 50 51 52 53 54 55 56 57
    if (wantedSectorSizes.isNotEmpty) {
      wantedSectorSizes.removeLast();
      updateEnabledState();
    }
  }

  void doUpdates() {
    int index = 0;
    while (index < actualSectorSizes.length && index < wantedSectorSizes.length && actualSectorSizes[index] == wantedSectorSizes[index])
      index += 1;
58
    final RenderSectorRing ring = sectors.child! as RenderSectorRing;
59
    while (index < actualSectorSizes.length) {
60
      ring.remove(ring.lastChild!);
61 62 63
      actualSectorSizes.removeLast();
    }
    while (index < wantedSectorSizes.length) {
64 65
      final Color color = Color(((0xFF << 24) + rand.nextInt(0xFFFFFF)) | 0x808080);
      ring.add(RenderSolidColor(color, desiredDeltaTheta: wantedSectorSizes[index]));
66 67 68
      actualSectorSizes.add(wantedSectorSizes[index]);
      index += 1;
    }
69 70
  }

71
  static RenderBoxToRenderSectorAdapter initSector(Color color) {
72 73 74 75 76
    final RenderSectorRing ring = RenderSectorRing(padding: 1.0);
    ring.add(RenderSolidColor(const Color(0xFF909090), desiredDeltaTheta: kTwoPi * 0.15));
    ring.add(RenderSolidColor(const Color(0xFF909090), desiredDeltaTheta: kTwoPi * 0.15));
    ring.add(RenderSolidColor(color, desiredDeltaTheta: kTwoPi * 0.2));
    return RenderBoxToRenderSectorAdapter(
77
      innerRadius: 5.0,
78
      child: ring,
79 80 81 82 83
    );
  }
  RenderBoxToRenderSectorAdapter sectorAddIcon = initSector(const Color(0xFF00DD00));
  RenderBoxToRenderSectorAdapter sectorRemoveIcon = initSector(const Color(0xFFDD0000));

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

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

152
  @override
Hixie's avatar
Hixie committed
153
  Widget build(BuildContext context) {
154 155
    return MaterialApp(
      theme: ThemeData.light(),
Hixie's avatar
Hixie committed
156
      title: 'Sector Layout',
157 158
      home: Scaffold(
        appBar: AppBar(
159
          title: const Text('Sector Layout in a Widget Tree'),
160
        ),
161 162
        body: buildBody(),
      ),
163 164 165 166 167
    );
  }
}

void main() {
168
  runApp(const SectorApp());
169
}