|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +/// |
| 4 | +/// 字符间隔默认时长 |
| 5 | +/// |
| 6 | +const int _kDefaultMillSeconds = 300; |
| 7 | + |
| 8 | +/// |
| 9 | +/// 默认光标 |
| 10 | +/// |
| 11 | +const Widget _kDefaultCursor = const _DefaultCursor(); |
| 12 | + |
| 13 | +/// |
| 14 | +/// desc: WriteText 是逐步显示文本的动画组件,像手写一样的效果。 |
| 15 | +/// |
| 16 | +class WriteText extends StatefulWidget { |
| 17 | + /// |
| 18 | + /// 数据 |
| 19 | + /// |
| 20 | + final String data; |
| 21 | + |
| 22 | + /// |
| 23 | + /// 是否显示光标 |
| 24 | + /// |
| 25 | + final bool showCursor; |
| 26 | + |
| 27 | + /// |
| 28 | + /// 光标组件 |
| 29 | + /// |
| 30 | + final Widget cursor; |
| 31 | + |
| 32 | + /// |
| 33 | + /// 字符间隔时长 |
| 34 | + /// |
| 35 | + final int perMillSeconds; |
| 36 | + |
| 37 | + /// |
| 38 | + /// 激活状态文本的样式 |
| 39 | + /// |
| 40 | + final TextStyle textStyle; |
| 41 | + |
| 42 | + /// |
| 43 | + /// 是否自动启动 |
| 44 | + /// |
| 45 | + final bool autoStart; |
| 46 | + |
| 47 | + /// |
| 48 | + /// 控制器 |
| 49 | + /// |
| 50 | + final WriteTextController controller; |
| 51 | + |
| 52 | + const WriteText({ |
| 53 | + Key key, |
| 54 | + @required this.data, |
| 55 | + this.controller, |
| 56 | + this.showCursor = true, |
| 57 | + this.cursor = _kDefaultCursor, |
| 58 | + this.perMillSeconds = _kDefaultMillSeconds, |
| 59 | + this.textStyle, |
| 60 | + this.autoStart = true, |
| 61 | + }) : assert(data != null, 'data cannot be null'), |
| 62 | + assert(perMillSeconds != null, 'perDuration cannot be null'), |
| 63 | + super(key: key); |
| 64 | + |
| 65 | + @override |
| 66 | + _WriteTextState createState() => _WriteTextState(); |
| 67 | +} |
| 68 | + |
| 69 | +class _WriteTextState extends State<WriteText> |
| 70 | + with SingleTickerProviderStateMixin { |
| 71 | + AnimationController _animationController; |
| 72 | + |
| 73 | + @override |
| 74 | + void initState() { |
| 75 | + _animationController = AnimationController( |
| 76 | + vsync: this, |
| 77 | + duration: |
| 78 | + Duration(milliseconds: widget.perMillSeconds * widget.data.length)); |
| 79 | + if (widget.autoStart) { |
| 80 | + _animationController.forward(); |
| 81 | + } |
| 82 | + if (widget.controller != null) { |
| 83 | + widget.controller._setStepTextState(this); |
| 84 | + } |
| 85 | + super.initState(); |
| 86 | + } |
| 87 | + |
| 88 | + /// |
| 89 | + /// 启动 |
| 90 | + /// |
| 91 | + start() { |
| 92 | + _animationController.forward(); |
| 93 | + } |
| 94 | + |
| 95 | + /// |
| 96 | + /// 停止 |
| 97 | + /// |
| 98 | + stop() { |
| 99 | + _animationController.stop(); |
| 100 | + } |
| 101 | + |
| 102 | + @override |
| 103 | + void dispose() { |
| 104 | + _animationController.dispose(); |
| 105 | + super.dispose(); |
| 106 | + } |
| 107 | + |
| 108 | + @override |
| 109 | + Widget build(BuildContext context) { |
| 110 | + return AnimatedBuilder( |
| 111 | + animation: _animationController, |
| 112 | + builder: (BuildContext context, Widget child) { |
| 113 | + int endIndex = |
| 114 | + (widget.data.length * _animationController.value).floor(); |
| 115 | + var text = widget.data.substring(0, endIndex); |
| 116 | + return RichText( |
| 117 | + text: TextSpan( |
| 118 | + style: DefaultTextStyle.of(context).style, |
| 119 | + children: <InlineSpan>[ |
| 120 | + TextSpan( |
| 121 | + text: '$text ', style: widget.textStyle ?? TextStyle()), |
| 122 | + if (widget.showCursor) |
| 123 | + WidgetSpan( |
| 124 | + child: StepTextCursor( |
| 125 | + cursor: widget.cursor ?? _kDefaultCursor, |
| 126 | + )), |
| 127 | + ]), |
| 128 | + ); |
| 129 | + }, |
| 130 | + ); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +class StepTextCursor extends StatefulWidget { |
| 135 | + final Widget cursor; |
| 136 | + |
| 137 | + const StepTextCursor({Key key, this.cursor}) : super(key: key); |
| 138 | + |
| 139 | + @override |
| 140 | + _StepTextCursorState createState() => _StepTextCursorState(); |
| 141 | +} |
| 142 | + |
| 143 | +class _StepTextCursorState extends State<StepTextCursor> |
| 144 | + with SingleTickerProviderStateMixin { |
| 145 | + AnimationController _controller; |
| 146 | + |
| 147 | + @override |
| 148 | + void initState() { |
| 149 | + _controller = |
| 150 | + AnimationController(vsync: this, duration: Duration(milliseconds: 300)) |
| 151 | + ..addStatusListener((status) { |
| 152 | + if (status == AnimationStatus.completed) { |
| 153 | + _controller.reverse(); |
| 154 | + } else if (status == AnimationStatus.dismissed) { |
| 155 | + _controller.forward(); |
| 156 | + } |
| 157 | + }); |
| 158 | + _controller.forward(); |
| 159 | + super.initState(); |
| 160 | + } |
| 161 | + |
| 162 | + @override |
| 163 | + void dispose() { |
| 164 | + _controller.dispose(); |
| 165 | + super.dispose(); |
| 166 | + } |
| 167 | + |
| 168 | + @override |
| 169 | + Widget build(BuildContext context) { |
| 170 | + return AnimatedBuilder( |
| 171 | + animation: _controller, |
| 172 | + builder: (BuildContext context, Widget child) { |
| 173 | + return Opacity( |
| 174 | + opacity: _controller.value, |
| 175 | + child: widget.cursor, |
| 176 | + ); |
| 177 | + }); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +class _DefaultCursor extends StatelessWidget { |
| 182 | + const _DefaultCursor(); |
| 183 | + |
| 184 | + @override |
| 185 | + Widget build(BuildContext context) { |
| 186 | + return Container( |
| 187 | + width: 12, |
| 188 | + height: 1, |
| 189 | + color: Theme.of(context).primaryColor, |
| 190 | + ); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +class WriteTextController { |
| 195 | + _WriteTextState _stepTextState; |
| 196 | + |
| 197 | + void _setStepTextState(_WriteTextState __stepTextState) { |
| 198 | + this._stepTextState = __stepTextState; |
| 199 | + } |
| 200 | + |
| 201 | + start() { |
| 202 | + _stepTextState.start(); |
| 203 | + } |
| 204 | + |
| 205 | + stop() { |
| 206 | + _stepTextState.stop(); |
| 207 | + } |
| 208 | +} |
0 commit comments