Skip to content

Commit e3d4170

Browse files
add write text
1 parent 9ebcd2f commit e3d4170

File tree

8 files changed

+371
-0
lines changed

8 files changed

+371
-0
lines changed

write_text/.gitignore

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
.dart_tool/
26+
.flutter-plugins
27+
.flutter-plugins-dependencies
28+
.packages
29+
.pub-cache/
30+
.pub/
31+
build/
32+
33+
# Android related
34+
**/android/**/gradle-wrapper.jar
35+
**/android/.gradle
36+
**/android/captures/
37+
**/android/gradlew
38+
**/android/gradlew.bat
39+
**/android/local.properties
40+
**/android/**/GeneratedPluginRegistrant.java
41+
42+
# iOS/XCode related
43+
**/ios/**/*.mode1v3
44+
**/ios/**/*.mode2v3
45+
**/ios/**/*.moved-aside
46+
**/ios/**/*.pbxuser
47+
**/ios/**/*.perspectivev3
48+
**/ios/**/*sync/
49+
**/ios/**/.sconsign.dblite
50+
**/ios/**/.tags*
51+
**/ios/**/.vagrant/
52+
**/ios/**/DerivedData/
53+
**/ios/**/Icon?
54+
**/ios/**/Pods/
55+
**/ios/**/.symlinks/
56+
**/ios/**/profile
57+
**/ios/**/xcuserdata
58+
**/ios/.generated/
59+
**/ios/Flutter/App.framework
60+
**/ios/Flutter/Flutter.framework
61+
**/ios/Flutter/Flutter.podspec
62+
**/ios/Flutter/Generated.xcconfig
63+
**/ios/Flutter/app.flx
64+
**/ios/Flutter/app.zip
65+
**/ios/Flutter/flutter_assets/
66+
**/ios/Flutter/flutter_export_environment.sh
67+
**/ios/ServiceDefinitions.json
68+
**/ios/Runner/GeneratedPluginRegistrant.*
69+
70+
# Exceptions to above rules.
71+
!**/ios/**/default.mode1v3
72+
!**/ios/**/default.mode2v3
73+
!**/ios/**/default.pbxuser
74+
!**/ios/**/default.perspectivev3

write_text/.metadata

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: f30b7f4db93ee747cd727df747941a28ead25ff5
8+
channel: stable
9+
10+
project_type: package

write_text/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [0.0.1] - TODO: Add release date.
2+
3+
* TODO: Describe initial release.

write_text/LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TODO: Add your license here.

write_text/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# write_text
2+
3+
A new Flutter package.
4+
5+
## Getting Started
6+
7+
This project is a starting point for a Dart
8+
[package](https://flutter.dev/developing-packages/),
9+
a library module containing code that can be shared easily across
10+
multiple Flutter or Dart projects.
11+
12+
For help getting started with Flutter, view our
13+
[online documentation](https://flutter.dev/docs), which offers tutorials,
14+
samples, guidance on mobile development, and a full API reference.

write_text/lib/write_text.dart

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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+
}

write_text/pubspec.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: write_text
2+
description: WriteText 是逐步显示文本的动画组件,像手写一样的效果。
3+
version: 0.0.1
4+
author:
5+
homepage:
6+
7+
environment:
8+
sdk: ">=2.7.0 <3.0.0"
9+
10+
dependencies:
11+
flutter:
12+
sdk: flutter
13+
14+
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
17+
18+
# For information on the generic Dart part of this file, see the
19+
# following page: https://dart.dev/tools/pub/pubspec
20+
21+
# The following section is specific to Flutter.
22+
flutter:
23+
24+
# To add assets to your package, add an assets section, like this:
25+
# assets:
26+
# - images/a_dot_burr.jpeg
27+
# - images/a_dot_ham.jpeg
28+
#
29+
# For details regarding assets in packages, see
30+
# https://flutter.dev/assets-and-images/#from-packages
31+
#
32+
# An image asset can refer to one or more resolution-specific "variants", see
33+
# https://flutter.dev/assets-and-images/#resolution-aware.
34+
35+
# To add custom fonts to your package, add a fonts section here,
36+
# in this "flutter" section. Each entry in this list should have a
37+
# "family" key with the font family name, and a "fonts" key with a
38+
# list giving the asset and other descriptors for the font. For
39+
# example:
40+
# fonts:
41+
# - family: Schyler
42+
# fonts:
43+
# - asset: fonts/Schyler-Regular.ttf
44+
# - asset: fonts/Schyler-Italic.ttf
45+
# style: italic
46+
# - family: Trajan Pro
47+
# fonts:
48+
# - asset: fonts/TrajanPro.ttf
49+
# - asset: fonts/TrajanPro_Bold.ttf
50+
# weight: 700
51+
#
52+
# For details regarding fonts in packages, see
53+
# https://flutter.dev/custom-fonts/#from-packages

write_text/test/write_text_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
3+
import 'package:write_text/write_text.dart';
4+
5+
void main() {
6+
test('adds one to input values', () {
7+
});
8+
}

0 commit comments

Comments
 (0)