sectors.dart 5.92 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(),
15 16 17
  );
}

18
class SectorApp extends StatefulWidget {
19
  const SectorApp({super.key});
20

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
        deltaTheta = kTwoPi - currentTheta;
40
      } else {
41
        deltaTheta = math.pi * rand.nextDouble() / 5.0 + 0.05;
42
      }
43 44 45
      wantedSectorSizes.add(deltaTheta);
      updateEnabledState();
    }
46 47 48
  }

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

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

73
  static RenderBoxToRenderSectorAdapter initSector(Color color) {
74 75 76 77 78
    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(
79
      innerRadius: 5.0,
80
      child: ring,
81 82 83 84 85
    );
  }
  RenderBoxToRenderSectorAdapter sectorAddIcon = initSector(const Color(0xFF00DD00));
  RenderBoxToRenderSectorAdapter sectorRemoveIcon = initSector(const Color(0xFFDD0000));

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

95 96 97 98 99 100 101
  void recursivelyDisposeChildren(RenderObject parent) {
    parent.visitChildren((RenderObject child) {
      recursivelyDisposeChildren(child);
      child.dispose();
    });
  }

102
  Widget buildBody() {
103
    return Column(
104
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
105
      children: <Widget>[
106
        Container(
107
          padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 25.0),
108
          child: Row(
109
            mainAxisAlignment: MainAxisAlignment.spaceAround,
110
            children: <Widget>[
111
              ElevatedButton(
112
                onPressed: _enabledAdd ? addSector : null,
113 114
                child: IntrinsicWidth(
                  child: Row(
115
                    children: <Widget>[
116
                      Container(
117 118
                        padding: const EdgeInsets.all(4.0),
                        margin: const EdgeInsets.only(right: 10.0),
119 120 121 122 123 124
                        child: WidgetToRenderBoxAdapter(
                          renderBox: sectorAddIcon,
                          onUnmount: () {
                            recursivelyDisposeChildren(sectorAddIcon);
                          },
                        ),
125
                      ),
126
                      const Text('ADD SECTOR'),
127 128 129
                    ],
                  ),
                ),
130
              ),
131
              ElevatedButton(
132
                onPressed: _enabledRemove ? removeSector : null,
133 134
                child: IntrinsicWidth(
                  child: Row(
135
                    children: <Widget>[
136
                      Container(
137 138
                        padding: const EdgeInsets.all(4.0),
                        margin: const EdgeInsets.only(right: 10.0),
139 140 141 142 143 144
                        child: WidgetToRenderBoxAdapter(
                          renderBox: sectorRemoveIcon,
                          onUnmount: () {
                            recursivelyDisposeChildren(sectorRemoveIcon);
                          },
                        ),
145
                      ),
146
                      const Text('REMOVE SECTOR'),
147 148 149
                    ],
                  ),
                ),
150
              ),
151
            ],
152
          ),
153
        ),
154 155
        Expanded(
          child: Container(
156
            margin: const EdgeInsets.all(8.0),
157
            decoration: BoxDecoration(
158
              border: Border.all(),
159
            ),
160
            padding: const EdgeInsets.all(8.0),
161
            child: WidgetToRenderBoxAdapter(
162
              renderBox: sectors,
163
              onBuild: doUpdates,
164 165 166
              onUnmount: () {
                recursivelyDisposeChildren(sectors);
              },
167 168
            ),
          ),
169 170
        ),
      ],
171 172 173
    );
  }

174
  @override
Hixie's avatar
Hixie committed
175
  Widget build(BuildContext context) {
176 177
    return MaterialApp(
      theme: ThemeData.light(),
Hixie's avatar
Hixie committed
178
      title: 'Sector Layout',
179 180
      home: Scaffold(
        appBar: AppBar(
181
          title: const Text('Sector Layout in a Widget Tree'),
182
        ),
183 184
        body: buildBody(),
      ),
185 186 187 188 189
    );
  }
}

void main() {
190
  runApp(const SectorApp());
191
}