2024-12-09 01:40:39 +08:00
|
|
|
import 'dart:async';
|
2025-03-12 17:15:31 +08:00
|
|
|
import 'dart:math';
|
2024-12-09 01:40:39 +08:00
|
|
|
|
|
|
|
|
import 'package:fl_clash/common/common.dart';
|
|
|
|
|
import 'package:fl_clash/models/models.dart';
|
2025-03-12 17:15:31 +08:00
|
|
|
import 'package:fl_clash/widgets/fade_box.dart';
|
2024-12-09 01:40:39 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
|
|
class MessageManager extends StatefulWidget {
|
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
|
|
const MessageManager({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.child,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<MessageManager> createState() => MessageManagerState();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-12 17:15:31 +08:00
|
|
|
class MessageManagerState extends State<MessageManager> {
|
2024-12-09 01:40:39 +08:00
|
|
|
final _messagesNotifier = ValueNotifier<List<CommonMessage>>([]);
|
2025-03-12 17:15:31 +08:00
|
|
|
final List<CommonMessage> _bufferMessages = [];
|
|
|
|
|
Completer<bool>? _messageIngCompleter;
|
2024-12-09 01:40:39 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_messagesNotifier.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-12 17:15:31 +08:00
|
|
|
Future<void> message(String text) async {
|
2024-12-09 01:40:39 +08:00
|
|
|
final commonMessage = CommonMessage(
|
|
|
|
|
id: other.uuidV4,
|
|
|
|
|
text: text,
|
|
|
|
|
);
|
2025-03-12 17:15:31 +08:00
|
|
|
_bufferMessages.add(commonMessage);
|
|
|
|
|
_showMessage();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_showMessage() async {
|
|
|
|
|
if (_messageIngCompleter?.isCompleted == false) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while (_bufferMessages.isNotEmpty) {
|
|
|
|
|
final commonMessage = _bufferMessages.removeAt(0);
|
|
|
|
|
_messagesNotifier.value = List.from(_messagesNotifier.value)
|
|
|
|
|
..add(
|
|
|
|
|
commonMessage,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
_messageIngCompleter = Completer();
|
|
|
|
|
await Future.delayed(Duration(seconds: 1));
|
|
|
|
|
Future.delayed(commonMessage.duration, () {
|
|
|
|
|
_handleRemove(commonMessage);
|
|
|
|
|
});
|
|
|
|
|
_messageIngCompleter?.complete(true);
|
|
|
|
|
}
|
2024-12-09 01:40:39 +08:00
|
|
|
}
|
|
|
|
|
|
2025-01-13 19:08:17 +08:00
|
|
|
_handleRemove(CommonMessage commonMessage) async {
|
2024-12-09 01:40:39 +08:00
|
|
|
_messagesNotifier.value = List<CommonMessage>.from(_messagesNotifier.value)
|
|
|
|
|
..remove(commonMessage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return Stack(
|
|
|
|
|
children: [
|
|
|
|
|
widget.child,
|
2025-03-12 17:15:31 +08:00
|
|
|
ValueListenableBuilder(
|
|
|
|
|
valueListenable: _messagesNotifier,
|
|
|
|
|
builder: (_, messages, __) {
|
|
|
|
|
return FadeThroughBox(
|
|
|
|
|
alignment: Alignment.topRight,
|
|
|
|
|
child: messages.isEmpty
|
|
|
|
|
? SizedBox()
|
|
|
|
|
: LayoutBuilder(
|
|
|
|
|
key: Key(messages.last.id),
|
|
|
|
|
builder: (_, constraints) {
|
|
|
|
|
return Card(
|
|
|
|
|
shape: const RoundedRectangleBorder(
|
|
|
|
|
borderRadius: BorderRadius.all(
|
|
|
|
|
Radius.circular(12.0),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
elevation: 10,
|
|
|
|
|
margin: EdgeInsets.only(
|
|
|
|
|
top: kToolbarHeight,
|
|
|
|
|
left: 12,
|
|
|
|
|
right: 12,
|
|
|
|
|
),
|
|
|
|
|
color: context.colorScheme.surfaceContainerHigh,
|
|
|
|
|
child: Container(
|
|
|
|
|
width: min(
|
|
|
|
|
constraints.maxWidth,
|
|
|
|
|
400,
|
|
|
|
|
),
|
|
|
|
|
padding: EdgeInsets.symmetric(
|
|
|
|
|
horizontal: 12,
|
|
|
|
|
vertical: 16,
|
|
|
|
|
),
|
|
|
|
|
child: Text(
|
|
|
|
|
messages.last.text,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
2024-12-09 01:40:39 +08:00
|
|
|
);
|
|
|
|
|
},
|
2025-03-12 17:15:31 +08:00
|
|
|
),
|
2024-12-09 01:40:39 +08:00
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|