-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
250 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
import 'package:fl_chart/fl_chart.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'dart:math' as math; | ||
|
||
class BarChartSample7 extends StatefulWidget { | ||
const BarChartSample7({Key? key}) : super(key: key); | ||
|
||
static const shadowColor = Color(0xFFCCCCCC); | ||
static const dataList = [ | ||
_BarData(Color(0xFFecb206), 18, 18), | ||
_BarData(Color(0xFFa8bd1a), 17, 8), | ||
_BarData(Color(0xFF17987b), 10, 15), | ||
_BarData(Color(0xFFb87d46), 2.5, 5), | ||
_BarData(Color(0xFF295ab5), 2, 2.5), | ||
_BarData(Color(0xFFea0107), 2, 2), | ||
]; | ||
|
||
@override | ||
State<BarChartSample7> createState() => _BarChartSample7State(); | ||
} | ||
|
||
class _BarChartSample7State extends State<BarChartSample7> { | ||
BarChartGroupData generateBarGroup( | ||
int x, | ||
Color color, | ||
double value, | ||
double shadowValue, | ||
) { | ||
return BarChartGroupData( | ||
x: x, | ||
barRods: [ | ||
BarChartRodData( | ||
toY: value, | ||
color: color, | ||
width: 6, | ||
), | ||
BarChartRodData( | ||
toY: shadowValue, | ||
color: BarChartSample7.shadowColor, | ||
width: 6, | ||
), | ||
], | ||
showingTooltipIndicators: touchedGroupIndex == x ? [0] : [], | ||
); | ||
} | ||
|
||
int touchedGroupIndex = -1; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Card( | ||
color: Colors.white, | ||
elevation: 4, | ||
child: Padding( | ||
padding: const EdgeInsets.all(24), | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: [ | ||
AspectRatio( | ||
aspectRatio: 1.4, | ||
child: BarChart( | ||
BarChartData( | ||
alignment: BarChartAlignment.spaceBetween, | ||
borderData: FlBorderData( | ||
show: true, | ||
border: const Border.symmetric( | ||
horizontal: BorderSide( | ||
color: Color(0xFFececec), | ||
), | ||
), | ||
), | ||
titlesData: FlTitlesData( | ||
show: true, | ||
leftTitles: AxisTitles( | ||
sideTitles: SideTitles( | ||
showTitles: true, | ||
reservedSize: 30, | ||
getTitles: (value, meta) { | ||
return Text( | ||
value.toInt().toString(), | ||
style: const TextStyle( | ||
color: Color(0xFF606060), | ||
), | ||
textAlign: TextAlign.left, | ||
); | ||
}, | ||
), | ||
), | ||
bottomTitles: AxisTitles( | ||
sideTitles: SideTitles( | ||
showTitles: true, | ||
reservedSize: 36, | ||
getTitles: (value, meta) { | ||
final index = value.toInt(); | ||
return Padding( | ||
padding: const EdgeInsets.only(top: 8.0), | ||
child: _IconWidget( | ||
color: BarChartSample7.dataList[index].color, | ||
isSelected: touchedGroupIndex == index, | ||
), | ||
); | ||
}, | ||
), | ||
), | ||
rightTitles: AxisTitles(), | ||
topTitles: AxisTitles(), | ||
), | ||
gridData: FlGridData( | ||
show: true, | ||
drawVerticalLine: false, | ||
getDrawingHorizontalLine: (value) => FlLine( | ||
color: const Color(0xFFececec), | ||
dashArray: null, | ||
strokeWidth: 1, | ||
), | ||
), | ||
barGroups: BarChartSample7.dataList.asMap().entries.map((e) { | ||
final index = e.key; | ||
final data = e.value; | ||
return generateBarGroup( | ||
index, data.color, data.value, data.shadowValue); | ||
}).toList(), | ||
maxY: 20, | ||
barTouchData: BarTouchData( | ||
enabled: true, | ||
handleBuiltInTouches: false, | ||
touchTooltipData: BarTouchTooltipData( | ||
tooltipBgColor: Colors.transparent, | ||
tooltipMargin: 0, | ||
getTooltipItem: ( | ||
BarChartGroupData group, | ||
int groupIndex, | ||
BarChartRodData rod, | ||
int rodIndex, | ||
) { | ||
return BarTooltipItem( | ||
rod.toY.toString(), | ||
TextStyle( | ||
fontWeight: FontWeight.bold, | ||
color: rod.color!, | ||
fontSize: 18, | ||
shadows: const [ | ||
Shadow( | ||
color: Colors.black26, | ||
blurRadius: 12, | ||
) | ||
]), | ||
); | ||
}), | ||
touchCallback: (event, response) { | ||
if (event.isInterestedForInteractions && | ||
response != null && | ||
response.spot != null) { | ||
setState(() { | ||
touchedGroupIndex = | ||
response.spot!.touchedBarGroupIndex; | ||
}); | ||
} else { | ||
setState(() { | ||
touchedGroupIndex = -1; | ||
}); | ||
} | ||
}, | ||
), | ||
), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class _BarData { | ||
final Color color; | ||
final double value; | ||
final double shadowValue; | ||
|
||
const _BarData(this.color, this.value, this.shadowValue); | ||
} | ||
|
||
class _IconWidget extends ImplicitlyAnimatedWidget { | ||
final Color color; | ||
final bool isSelected; | ||
|
||
const _IconWidget({ | ||
required this.color, | ||
required this.isSelected, | ||
}) : super(duration: const Duration(milliseconds: 300)); | ||
|
||
@override | ||
ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() => | ||
_IconWidgetState(); | ||
} | ||
|
||
class _IconWidgetState extends AnimatedWidgetBaseState<_IconWidget> { | ||
Tween<double>? _rotationTween; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final rotation = math.pi * 4 * _rotationTween!.evaluate(animation); | ||
final scale = 1 + _rotationTween!.evaluate(animation) * 0.5; | ||
return Transform( | ||
transform: Matrix4.rotationZ(rotation).scaled(scale, scale), | ||
origin: const Offset(14, 14), | ||
child: Icon( | ||
widget.isSelected ? Icons.face_retouching_natural : Icons.face, | ||
color: widget.color, | ||
size: 28, | ||
), | ||
); | ||
} | ||
|
||
@override | ||
void forEachTween(TweenVisitor<dynamic> visitor) { | ||
_rotationTween = visitor( | ||
_rotationTween, | ||
widget.isSelected ? 1.0 : 0.0, | ||
(dynamic value) => Tween<double>( | ||
begin: value, | ||
end: widget.isSelected ? 1.0 : 0.0, | ||
), | ||
) as Tween<double>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.