refactoring
This commit is contained in:
1 parent
11cba09e30
commit
6281d24c64
7 files changed
+20
-20
No files matched your search
@@ -1,84 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_circular_chart_two/flutter_circular_chart_two.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
home: AnimatedPieChartExample(),
|
||||
));
|
||||
}
|
||||
|
||||
final List<List<CircularStackEntry>> _quarterlyProfitPieData = [
|
||||
<CircularStackEntry>[
|
||||
CircularStackEntry(
|
||||
<CircularSegmentEntry>[
|
||||
CircularSegmentEntry(500.0, Colors.red[200], rankKey: 'Q1'),
|
||||
CircularSegmentEntry(1000.0, Colors.green[200], rankKey: 'Q2'),
|
||||
CircularSegmentEntry(2000.0, Colors.blue[200], rankKey: 'Q3'),
|
||||
CircularSegmentEntry(1000.0, Colors.yellow[200], rankKey: 'Q4'),
|
||||
],
|
||||
rankKey: 'Quarterly Profits',
|
||||
),
|
||||
],
|
||||
<CircularStackEntry>[
|
||||
CircularStackEntry(
|
||||
<CircularSegmentEntry>[
|
||||
CircularSegmentEntry(1500.0, Colors.red[200], rankKey: 'Q1'),
|
||||
CircularSegmentEntry(750.0, Colors.green[200], rankKey: 'Q2'),
|
||||
CircularSegmentEntry(2000.0, Colors.blue[200], rankKey: 'Q3'),
|
||||
CircularSegmentEntry(1000.0, Colors.yellow[200], rankKey: 'Q4'),
|
||||
],
|
||||
rankKey: 'Quarterly Profits',
|
||||
),
|
||||
],
|
||||
<CircularStackEntry>[
|
||||
CircularStackEntry(
|
||||
<CircularSegmentEntry>[
|
||||
CircularSegmentEntry(1800.0, Colors.red[200], rankKey: 'Q1'),
|
||||
CircularSegmentEntry(2900.0, Colors.green[200], rankKey: 'Q2'),
|
||||
CircularSegmentEntry(4000.0, Colors.blue[200], rankKey: 'Q3'),
|
||||
CircularSegmentEntry(7000.0, Colors.yellow[200], rankKey: 'Q4'),
|
||||
],
|
||||
rankKey: 'Quarterly Profits',
|
||||
),
|
||||
],
|
||||
];
|
||||
|
||||
class AnimatedPieChartExample extends StatefulWidget {
|
||||
@override
|
||||
_AnimatedPieChartExampleState createState() => _AnimatedPieChartExampleState();
|
||||
}
|
||||
|
||||
class _AnimatedPieChartExampleState extends State<AnimatedPieChartExample> {
|
||||
final GlobalKey<AnimatedCircularChartState> _chartKey = GlobalKey<AnimatedCircularChartState>();
|
||||
final _chartSize = const Size(300.0, 300.0);
|
||||
int sampleIndex = 0;
|
||||
|
||||
void _cycleSamples() {
|
||||
setState(() {
|
||||
sampleIndex++;
|
||||
List<CircularStackEntry> data = _quarterlyProfitPieData[sampleIndex % 3];
|
||||
_chartKey.currentState.updateData(data);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Quarterly Profit'),
|
||||
),
|
||||
body: Center(
|
||||
child: AnimatedCircularChart(
|
||||
key: _chartKey,
|
||||
size: _chartSize,
|
||||
initialChartData: _quarterlyProfitPieData[0],
|
||||
chartType: CircularChartType.Pie,
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
child: Icon(Icons.refresh),
|
||||
onPressed: _cycleSamples,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_circular_chart_two/flutter_circular_chart_two.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
home: AnimatedRadialChartExample(),
|
||||
));
|
||||
}
|
||||
|
||||
class AnimatedRadialChartExample extends StatefulWidget {
|
||||
@override
|
||||
_AnimatedRadialChartExampleState createState() => _AnimatedRadialChartExampleState();
|
||||
}
|
||||
|
||||
class _AnimatedRadialChartExampleState extends State<AnimatedRadialChartExample> {
|
||||
final GlobalKey<AnimatedCircularChartState> _chartKey = GlobalKey<AnimatedCircularChartState>();
|
||||
final _chartSize = const Size(200.0, 200.0);
|
||||
|
||||
double value = 50.0;
|
||||
Color labelColor = Colors.blue[200];
|
||||
|
||||
void _increment() {
|
||||
setState(() {
|
||||
value += 10;
|
||||
List<CircularStackEntry> data = _generateChartData(value);
|
||||
_chartKey.currentState.updateData(data);
|
||||
});
|
||||
}
|
||||
|
||||
void _decrement() {
|
||||
setState(() {
|
||||
value -= 10;
|
||||
List<CircularStackEntry> data = _generateChartData(value);
|
||||
_chartKey.currentState.updateData(data);
|
||||
});
|
||||
}
|
||||
|
||||
List<CircularStackEntry> _generateChartData(double value) {
|
||||
Color dialColor = Colors.blue[200];
|
||||
if (value < 0) {
|
||||
dialColor = Colors.red[200];
|
||||
} else if (value < 50) {
|
||||
dialColor = Colors.yellow[200];
|
||||
}
|
||||
labelColor = dialColor;
|
||||
|
||||
List<CircularStackEntry> data = <CircularStackEntry>[
|
||||
CircularStackEntry(
|
||||
<CircularSegmentEntry>[
|
||||
CircularSegmentEntry(
|
||||
value,
|
||||
dialColor,
|
||||
rankKey: 'percentage',
|
||||
)
|
||||
],
|
||||
rankKey: 'percentage',
|
||||
),
|
||||
];
|
||||
|
||||
if (value > 100) {
|
||||
labelColor = Colors.green[200];
|
||||
|
||||
data.add(CircularStackEntry(
|
||||
<CircularSegmentEntry>[
|
||||
CircularSegmentEntry(
|
||||
value - 100,
|
||||
Colors.green[200],
|
||||
rankKey: 'percentage',
|
||||
),
|
||||
],
|
||||
rankKey: 'percentage2',
|
||||
));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
TextStyle _labelStyle =
|
||||
Theme.of(context).textTheme.headline6.merge(TextStyle(color: labelColor));
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Percentage Dial'),
|
||||
),
|
||||
body: Column(
|
||||
children: <Widget>[
|
||||
Container(
|
||||
child: AnimatedCircularChart(
|
||||
key: _chartKey,
|
||||
size: _chartSize,
|
||||
initialChartData: _generateChartData(value),
|
||||
chartType: CircularChartType.Radial,
|
||||
edgeStyle: SegmentEdgeStyle.round,
|
||||
percentageValues: true,
|
||||
holeLabel: '$value%',
|
||||
labelStyle: _labelStyle,
|
||||
),
|
||||
),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: <Widget>[
|
||||
RaisedButton(
|
||||
onPressed: _decrement,
|
||||
child: const Icon(Icons.remove),
|
||||
shape: const CircleBorder(),
|
||||
color: Colors.red[200],
|
||||
textColor: Colors.white,
|
||||
),
|
||||
RaisedButton(
|
||||
onPressed: _increment,
|
||||
child: const Icon(Icons.add),
|
||||
shape: const CircleBorder(),
|
||||
color: Colors.blue[200],
|
||||
textColor: Colors.white,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_circular_chart_two/flutter_circular_chart_two.dart';
|
||||
import 'dart:math' as Math;
|
||||
|
||||
import 'color_palette.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MaterialApp(
|
||||
home: RandomizedRadialChartExample(),
|
||||
));
|
||||
}
|
||||
|
||||
class RandomizedRadialChartExample extends StatefulWidget {
|
||||
@override
|
||||
_RandomizedRadialChartExampleState createState() => _RandomizedRadialChartExampleState();
|
||||
}
|
||||
|
||||
class _RandomizedRadialChartExampleState extends State<RandomizedRadialChartExample> {
|
||||
final GlobalKey<AnimatedCircularChartState> _chartKey = GlobalKey<AnimatedCircularChartState>();
|
||||
final _chartSize = const Size(300.0, 300.0);
|
||||
final Math.Random random = Math.Random();
|
||||
List<CircularStackEntry> data;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
data = _generateRandomData();
|
||||
}
|
||||
|
||||
double value = 50.0;
|
||||
|
||||
void _randomize() {
|
||||
setState(() {
|
||||
data = _generateRandomData();
|
||||
_chartKey.currentState.updateData(data);
|
||||
});
|
||||
}
|
||||
|
||||
List<CircularStackEntry> _generateRandomData() {
|
||||
int stackCount = random.nextInt(10);
|
||||
List<CircularStackEntry> data = List.generate(stackCount, (i) {
|
||||
int segCount = random.nextInt(10);
|
||||
List<CircularSegmentEntry> segments = List.generate(segCount, (j) {
|
||||
Color randomColor = ColorPalette.primary.random(random);
|
||||
return CircularSegmentEntry(random.nextDouble(), randomColor);
|
||||
});
|
||||
return CircularStackEntry(segments);
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Randomized radial data'),
|
||||
),
|
||||
body: Center(
|
||||
child: AnimatedCircularChart(
|
||||
key: _chartKey,
|
||||
size: _chartSize,
|
||||
initialChartData: data,
|
||||
chartType: CircularChartType.Radial,
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _randomize,
|
||||
child: const Icon(Icons.refresh),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ColorPalette {
|
||||
static final ColorPalette primary = ColorPalette(<Color>[
|
||||
Colors.blue[400],
|
||||
Colors.blue[200],
|
||||
Colors.red[400],
|
||||
Colors.red[200],
|
||||
Colors.green[400],
|
||||
Colors.green[200],
|
||||
Colors.yellow[400],
|
||||
Colors.yellow[200],
|
||||
Colors.purple[400],
|
||||
Colors.purple[200],
|
||||
Colors.orange[400],
|
||||
Colors.orange[200],
|
||||
Colors.teal[400],
|
||||
Colors.teal[200],
|
||||
Colors.black,
|
||||
]);
|
||||
|
||||
ColorPalette(List<Color> colors) : _colors = colors {
|
||||
assert(colors.isNotEmpty);
|
||||
}
|
||||
|
||||
final List<Color> _colors;
|
||||
|
||||
Color operator [](int index) => _colors[index % length];
|
||||
|
||||
int get length => _colors.length;
|
||||
|
||||
Color random(Random random) => this[random.nextInt(length)];
|
||||
}
|
||||
Reference in new issue
Block a user