This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_sink.dart
188 lines (162 loc) · 5.03 KB
/
log_sink.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import 'package:flutter/material.dart';
/// [AppBar] action that shows a [Overlay] with log.
class LogActionWidget extends StatefulWidget {
/// Construct the [LogActionWidget]
const LogActionWidget({Key? key}) : super(key: key);
@override
_LogActionWidgetState createState() => _LogActionWidgetState();
}
class _LogActionWidgetState extends State<LogActionWidget> {
bool _isOverlayShowed = false;
OverlayEntry? _overlayEntry;
@override
void dispose() {
_removeOverlay();
super.dispose();
}
void _removeOverlay() {
if (_overlayEntry != null) {
_overlayEntry!.remove();
_overlayEntry = null;
}
}
@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () {
if (_isOverlayShowed) {
_removeOverlay();
} else {
_overlayEntry = OverlayEntry(builder: (c) {
return Positioned(
left: 0,
top: 0,
height: MediaQuery.of(context).size.height * 0.5,
width: MediaQuery.of(context).size.width,
child: Container(
color: Colors.black87,
child: SafeArea(
bottom: false,
child: Material(
color: Colors.transparent,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Row(
mainAxisSize: MainAxisSize.max,
children: [
TextButton(
onPressed: () {
logSink.clear();
},
child: const Text(
'Clear log',
style: TextStyle(color: Colors.white),
),
),
Expanded(
child: Align(
alignment: Alignment.topRight,
child: IconButton(
color: Colors.transparent,
onPressed: () {
_removeOverlay();
_isOverlayShowed = !_isOverlayShowed;
},
icon: const Icon(
Icons.close,
color: Colors.white,
)),
),
),
],
),
const Expanded(
child: SingleChildScrollView(
child: LogWidget(),
),
)
],
),
),
),
),
);
});
Overlay.of(context)?.insert(_overlayEntry!);
}
_isOverlayShowed = !_isOverlayShowed;
// setState(() {
// });
},
child: const Text(
'Log',
style: TextStyle(color: Colors.white),
));
}
}
/// LogWidget
class LogWidget extends StatefulWidget {
/// Construct the [LogWidget]
const LogWidget({
Key? key,
this.logSink,
this.textStyle = const TextStyle(fontSize: 15, color: Colors.white),
}) : super(key: key);
/// This [LogSink] is used to add log.
final LogSink? logSink;
/// The text style of the log.
final TextStyle textStyle;
@override
_LogWidgetState createState() => _LogWidgetState();
}
class _LogWidgetState extends State<LogWidget> {
VoidCallback? _listener;
late final LogSink _logSink;
@override
void initState() {
super.initState();
_logSink = widget.logSink ?? _defaultLogSink;
_listener ??= () {
setState(() {});
};
_logSink.addListener(_listener!);
}
@override
void dispose() {
if (_listener != null) {
_logSink.removeListener(_listener!);
_listener = null;
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return Text(
_logSink.output(),
style: widget.textStyle,
);
}
}
/// Class that add and update the log in [LogActionWidget]
class LogSink extends ChangeNotifier {
final StringBuffer _stringBuffer = StringBuffer();
/// Add log to [LogActionWidget]
void log(String log) {
_stringBuffer.writeln(log);
notifyListeners();
}
/// Get all logs
String output() {
return _stringBuffer.toString();
}
/// Clear logs
void clear() {
_stringBuffer.clear();
notifyListeners();
}
}
final LogSink _defaultLogSink = LogSink();
/// The global [LogSink]
LogSink get logSink => _defaultLogSink;