-
Notifications
You must be signed in to change notification settings - Fork 505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: BrnProgressChart 设置颜色,背景色,以及动画无效 #326
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
360dcfc
fix BrnProgressChart 设置颜色,背景色,以及动画无效
leftcoding 5435c56
fix BrnProgressChart 设置颜色,背景色,以及动画无效
leftcoding d31b51f
Merge branch 'LianjiaTech:3.x' into 3.x
leftcoding 0df15d8
fix: BrnProgressChart 设置颜色,背景色,以及动画无效
leftcoding 3595339
Merge branch '3.x' of github.com:leftcoding/bruno into 3.x
leftcoding f55014b
fix: BrnProgressChart 设置颜色,背景色,以及动画无效
leftcoding b082c1e
fix: BrnProgressChart 设置颜色,背景色,以及动画无效
leftcoding File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
|
||
|
||
import 'package:bruno/src/components/charts/brn_progress_chart/brn_progress_chart_painter.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
/// 在进度条上展示的 Widget | ||
|
@@ -36,6 +37,12 @@ class BrnProgressChart extends StatefulWidget { | |
/// 是否展示动画,默认 false | ||
final bool showAnimation; | ||
|
||
/// 动画时长,默认 Duration(milliseconds: 250) | ||
final Duration duration; | ||
|
||
/// 进度条是否从上次的值开始 | ||
final bool isFromLastValue; | ||
|
||
const BrnProgressChart( | ||
{Key? key, | ||
this.width = 0, | ||
|
@@ -46,7 +53,9 @@ class BrnProgressChart extends StatefulWidget { | |
this.brnProgressIndicatorBuilder, | ||
this.colors = const [Colors.blueAccent, Colors.blue], | ||
this.backgroundColor = Colors.lightBlueAccent, | ||
this.showAnimation = false}) | ||
this.showAnimation = false, | ||
this.isFromLastValue = false, | ||
this.duration = const Duration(milliseconds: 250),}) | ||
: assert(0 <= value && value <= 1, 'value 必须在 0 到 1 之间'), | ||
super(key: key); | ||
|
||
|
@@ -59,59 +68,125 @@ class BrnProgressChart extends StatefulWidget { | |
class BrnProgressChartState extends State<BrnProgressChart> | ||
with SingleTickerProviderStateMixin { | ||
late Animation<double> _animation; | ||
AnimationController? _animationController; | ||
double _value = 0; | ||
late AnimationController _animationController = | ||
AnimationController(vsync: this); | ||
double _lastValue = 0; | ||
|
||
bool get _isAnimation => widget.showAnimation; | ||
|
||
void _initAnimation() { | ||
final double begin = widget.isFromLastValue ? _lastValue : 0; | ||
final double end = widget.value; | ||
final Tween<double> tween = Tween<double>(begin: begin, end: end); | ||
_animationController.duration = | ||
_isAnimation ? widget.duration : Duration.zero; | ||
_animation = tween.animate(_animationController); | ||
_animationController.reset(); | ||
_animationController.forward(); | ||
_lastValue = end; | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
if (widget.showAnimation) { | ||
_animationController = AnimationController( | ||
vsync: this, duration: Duration(milliseconds: 250)); | ||
Tween tween = Tween<double>(begin: 0, end: widget.value); | ||
_animation = tween.animate(_animationController!) as Animation<double>; | ||
_animation.addListener(() { | ||
setState(() { | ||
_value = _animation.value; | ||
}); | ||
}); | ||
_animationController!.forward(); | ||
} else { | ||
_value = widget.value; | ||
_initAnimation(); | ||
} | ||
|
||
@override | ||
void didUpdateWidget(covariant BrnProgressChart oldWidget) { | ||
super.didUpdateWidget(oldWidget); | ||
if (oldWidget.value != widget.value) { | ||
if (_animationController.isAnimating == true) { | ||
_animationController.stop(); | ||
} | ||
_initAnimation(); | ||
} | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_animationController?.dispose(); | ||
_animationController.dispose(); | ||
super.dispose(); | ||
} | ||
|
||
Widget _indicatorWidgetBuilder(BuildContext context, double value) { | ||
return Text( | ||
'$value', | ||
style: widget.textStyle, | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final double _value = widget.value; | ||
return Stack( | ||
children: <Widget>[ | ||
CustomPaint( | ||
size: Size(widget.width, widget.height), | ||
painter: BrnProgressChartPainter(value: _value), | ||
painter: BrnProgressChartPainter( | ||
value: _value, | ||
animation: _animation, | ||
backgroundColor: widget.backgroundColor, | ||
colors: widget.colors, | ||
), | ||
), | ||
Container( | ||
width: widget.width, | ||
height: widget.height, | ||
padding: EdgeInsets.only(left: widget.indicatorLeftPadding), | ||
alignment: Alignment.centerLeft, | ||
child: null != widget.brnProgressIndicatorBuilder | ||
? widget.brnProgressIndicatorBuilder!(context, _value) | ||
: _indicatorWidgetBuilder(context, _value), | ||
child: IndicatorWidgetBuilder( | ||
notifier: _animation, | ||
value: _value, | ||
textStyle: widget.textStyle, | ||
brnProgressIndicatorBuilder: widget.brnProgressIndicatorBuilder, | ||
), | ||
) | ||
], | ||
); | ||
} | ||
} | ||
|
||
class IndicatorWidgetBuilder extends StatefulWidget { | ||
const IndicatorWidgetBuilder({ | ||
Key? key, | ||
required this.value, | ||
required this.textStyle, | ||
this.notifier, | ||
this.brnProgressIndicatorBuilder, | ||
}) : super(key: key); | ||
|
||
final ValueListenable<double>? notifier; | ||
final double value; | ||
final TextStyle textStyle; | ||
final BrnProgressIndicatorBuilder? brnProgressIndicatorBuilder; | ||
|
||
@override | ||
State<IndicatorWidgetBuilder> createState() => _IndicatorWidgetBuilderState(); | ||
} | ||
|
||
class _IndicatorWidgetBuilderState extends State<IndicatorWidgetBuilder> { | ||
late double _value; | ||
|
||
void _changeListener() { | ||
final double value = widget.notifier?.value ?? widget.value; | ||
if(!mounted) return; | ||
setState(() { | ||
_value = value; | ||
}); | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_value = widget.value; | ||
widget.notifier?.addListener(_changeListener); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
widget.notifier?.removeListener(_changeListener); | ||
super.dispose(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final BrnProgressIndicatorBuilder? builder = | ||
widget.brnProgressIndicatorBuilder; | ||
return builder?.call(context, _value) ?? | ||
Text('文本:$_value', style: widget.textStyle); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 是的,我处理下 |
||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是不是在init里面初始化好些
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我是觉得用late 会更好点