Skip to content

Commit 8f22b0a

Browse files
committed
[function] 完成语音播报动画
Signed-off-by: xiaweizi <1012126908@qq.com>
1 parent e417d3a commit 8f22b0a

File tree

4 files changed

+95
-12
lines changed

4 files changed

+95
-12
lines changed

assets/images/play.png

565 Bytes
Loading

lib/views/pages/home/real_time_temp.dart

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
66
import 'package:flutter_dynamic_weather/app/utils/print_utils.dart';
77
import 'package:flutter_dynamic_weather/event/change_index_envent.dart';
88
import 'package:flutter_dynamic_weather/views/app/flutter_app.dart';
9+
import 'package:flutter_dynamic_weather/views/pages/home/speak_anim.dart';
910
import 'package:flutter_tts/flutter_tts.dart';
1011

1112
class RealTimeTempView extends StatefulWidget {
@@ -106,6 +107,7 @@ class _RealTimeTempViewState extends State<RealTimeTempView> {
106107

107108
@override
108109
Widget build(BuildContext context) {
110+
var color = Colors.white;
109111
return GestureDetector(
110112
child: Container(
111113
child: Row(
@@ -118,27 +120,29 @@ class _RealTimeTempViewState extends State<RealTimeTempView> {
118120
Text(
119121
"${widget.temp}",
120122
style: TextStyle(
121-
fontSize: 150,
122-
fontWeight: FontWeight.w400,
123-
color: Colors.white),
123+
fontSize: 150, fontWeight: FontWeight.w400, color: color),
124124
),
125125
Text(
126126
"°",
127127
style: TextStyle(
128-
fontSize: 50,
129-
fontWeight: FontWeight.w500,
130-
color: Colors.white),
128+
fontSize: 50, fontWeight: FontWeight.w500, color: color),
131129
),
132130
Container(
133-
alignment: Alignment(1, 0.8),
134-
width: 20,
131+
alignment: Alignment(1, 0.9),
132+
width: 30,
135133
height: 150,
136-
child: Icon(
137-
Icons.ac_unit,
138-
color: Colors.white,
134+
child: AnimatedCrossFade(
135+
firstChild: Image.asset(
136+
"assets/images/play.png",
137+
color: color,
138+
),
139+
secondChild: SpeakAnim(),
140+
crossFadeState: ttsState == TtsState.playing
141+
? CrossFadeState.showSecond
142+
: CrossFadeState.showFirst,
143+
duration: Duration(milliseconds: 150),
139144
),
140145
),
141-
Text("$ttsState"),
142146
],
143147
),
144148
),
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import 'dart:math';
2+
3+
import 'package:flutter/material.dart';
4+
5+
class SpeakAnim extends StatefulWidget {
6+
@override
7+
_SpeakAnimState createState() => _SpeakAnimState();
8+
}
9+
10+
class _SpeakAnimState extends State<SpeakAnim>
11+
with SingleTickerProviderStateMixin {
12+
AnimationController _controller;
13+
Animation<double> animation;
14+
15+
@override
16+
void initState() {
17+
_controller = new AnimationController(
18+
vsync: this, duration: Duration(seconds: 6));
19+
animation = Tween<double>(begin: 0, end: 30).animate(_controller)
20+
..addListener(() {
21+
setState(() {});
22+
});
23+
_controller.repeat();
24+
super.initState();
25+
}
26+
27+
@override
28+
void deactivate() {
29+
_controller.stop();
30+
super.deactivate();
31+
}
32+
33+
@override
34+
void dispose() {
35+
_controller.dispose();
36+
super.dispose();
37+
}
38+
39+
@override
40+
Widget build(BuildContext context) {
41+
return Container(
42+
child: CustomPaint(
43+
painter: SpeakPainter(animation.value),
44+
size: Size(25, 15),
45+
),
46+
);
47+
}
48+
}
49+
50+
class SpeakPainter extends CustomPainter {
51+
52+
final double itemWidth = 3;
53+
Paint _paint = Paint();
54+
final double value;
55+
56+
SpeakPainter(this.value);
57+
58+
@override
59+
void paint(Canvas canvas, Size size) {
60+
var width = size.width;
61+
var height = size.height;
62+
_paint.color = Colors.white;
63+
_paint.style = PaintingStyle.fill;
64+
_paint.isAntiAlias = true;
65+
double startX = -width;
66+
int count = width ~/ itemWidth;
67+
for (int i = 0; i < count; i++) {
68+
double x = startX + itemWidth * 2 * i + value;
69+
double y = (sin(x) + 1) * height / 3 + height / 3;
70+
canvas.drawRRect(RRect.fromLTRBAndCorners(x, height - y, x + itemWidth, height, topLeft: Radius.circular(4), topRight: Radius.circular(4)), _paint);
71+
}
72+
}
73+
74+
@override
75+
bool shouldRepaint(CustomPainter oldDelegate) {
76+
return true;
77+
}
78+
}

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ flutter:
8282
- assets/images/typhoon.png
8383
- assets/images/thunder.webp
8484
- assets/images/sun.webp
85+
- assets/images/play.png
8586

8687
- assets/images/weather/
8788
- assets/images/lightning/

0 commit comments

Comments
 (0)