forked from SimformSolutionsPvtLtd/flutter_chatview
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvoice_message_view.dart
166 lines (148 loc) · 5.75 KB
/
voice_message_view.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import 'dart:async';
import 'package:audio_waveforms/audio_waveforms.dart';
import 'package:chatview/chatview.dart';
import 'package:chatview/src/models/voice_message_configuration.dart';
import 'package:chatview/src/widgets/reaction_widget.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
class VoiceMessageView extends StatefulWidget {
const VoiceMessageView({
Key? key,
required this.screenWidth,
required this.message,
required this.isMessageBySender,
this.inComingChatBubbleConfig,
this.outgoingChatBubbleConfig,
this.onMaxDuration,
this.messageReactionConfig,
this.config,
}) : super(key: key);
/// Provides configuration related to voice message.
final VoiceMessageConfiguration? config;
/// Allow user to set width of chat bubble.
final double screenWidth;
/// Provides message instance of chat.
final Message message;
final Function(int)? onMaxDuration;
/// Represents current message is sent by current user.
final bool isMessageBySender;
/// Provides configuration of reaction appearance in chat bubble.
final MessageReactionConfiguration? messageReactionConfig;
/// Provides configuration of chat bubble appearance from other user of chat.
final ChatBubble? inComingChatBubbleConfig;
/// Provides configuration of chat bubble appearance from current user of chat.
final ChatBubble? outgoingChatBubbleConfig;
@override
State<VoiceMessageView> createState() => _VoiceMessageViewState();
}
class _VoiceMessageViewState extends State<VoiceMessageView> {
late PlayerController controller;
late StreamSubscription<PlayerState> playerStateSubscription;
final ValueNotifier<PlayerState> _playerState =
ValueNotifier(PlayerState.stopped);
PlayerState get playerState => _playerState.value;
PlayerWaveStyle playerWaveStyle = const PlayerWaveStyle(scaleFactor: 70);
@override
void initState() {
super.initState();
controller = PlayerController()
..preparePlayer(
path: widget.message.message,
noOfSamples: widget.config?.playerWaveStyle
?.getSamplesForWidth(widget.screenWidth * 0.5) ??
playerWaveStyle.getSamplesForWidth(widget.screenWidth * 0.5),
).whenComplete(() => widget.onMaxDuration?.call(controller.maxDuration));
playerStateSubscription = controller.onPlayerStateChanged
.listen((state) => _playerState.value = state);
}
@override
void dispose() {
playerStateSubscription.cancel();
controller.dispose();
_playerState.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Stack(
clipBehavior: Clip.none,
children: [
Container(
decoration: widget.config?.decoration ??
BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: widget.isMessageBySender
? widget.outgoingChatBubbleConfig?.color
: widget.inComingChatBubbleConfig?.color,
),
padding: widget.config?.padding ??
const EdgeInsets.symmetric(horizontal: 8),
margin: widget.config?.margin ??
EdgeInsets.symmetric(
horizontal: 8,
vertical: widget.message.reaction.reactions.isNotEmpty ? 15 : 0,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
ValueListenableBuilder<PlayerState>(
builder: (context, state, child) {
return IconButton(
onPressed: _playOrPause,
icon:
state.isStopped || state.isPaused || state.isInitialised
? widget.config?.playIcon ??
const Icon(
Icons.play_arrow,
color: Colors.white,
)
: widget.config?.pauseIcon ??
const Icon(
Icons.stop,
color: Colors.white,
),
);
},
valueListenable: _playerState,
),
AudioFileWaveforms(
size: Size(widget.screenWidth * 0.50, 60),
playerController: controller,
waveformType: WaveformType.fitWidth,
playerWaveStyle:
widget.config?.playerWaveStyle ?? playerWaveStyle,
padding: widget.config?.waveformPadding ??
const EdgeInsets.only(right: 10),
margin: widget.config?.waveformMargin,
animationCurve: widget.config?.animationCurve ?? Curves.easeIn,
animationDuration: widget.config?.animationDuration ??
const Duration(milliseconds: 500),
enableSeekGesture: widget.config?.enableSeekGesture ?? true,
),
],
),
),
if (widget.message.reaction.reactions.isNotEmpty)
ReactionWidget(
isMessageBySender: widget.isMessageBySender,
reaction: widget.message.reaction,
messageReactionConfig: widget.messageReactionConfig,
),
],
);
}
void _playOrPause() {
assert(
defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.android,
"Voice messages are only supported with android and ios platform",
);
if (playerState.isInitialised ||
playerState.isPaused ||
playerState.isStopped) {
controller.startPlayer(finishMode: FinishMode.pause);
} else {
controller.pausePlayer();
}
}
}