log_sink.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import 'package:flutter/material.dart';
  2. /// [AppBar] action that shows a [Overlay] with log.
  3. class LogActionWidget extends StatefulWidget {
  4. /// Construct the [LogActionWidget]
  5. const LogActionWidget({Key? key}) : super(key: key);
  6. @override
  7. _LogActionWidgetState createState() => _LogActionWidgetState();
  8. }
  9. class _LogActionWidgetState extends State<LogActionWidget> {
  10. bool _isOverlayShowed = false;
  11. OverlayEntry? _overlayEntry;
  12. @override
  13. void dispose() {
  14. _removeOverlay();
  15. super.dispose();
  16. }
  17. void _removeOverlay() {
  18. if (_overlayEntry != null) {
  19. _overlayEntry!.remove();
  20. _overlayEntry = null;
  21. }
  22. }
  23. @override
  24. Widget build(BuildContext context) {
  25. return TextButton(
  26. onPressed: () {
  27. if (_isOverlayShowed) {
  28. _removeOverlay();
  29. } else {
  30. _overlayEntry = OverlayEntry(builder: (c) {
  31. return Positioned(
  32. left: 0,
  33. top: 0,
  34. height: MediaQuery.of(context).size.height * 0.5,
  35. width: MediaQuery.of(context).size.width,
  36. child: Container(
  37. color: Colors.black87,
  38. child: SafeArea(
  39. bottom: false,
  40. child: Material(
  41. color: Colors.transparent,
  42. child: Column(
  43. crossAxisAlignment: CrossAxisAlignment.start,
  44. mainAxisSize: MainAxisSize.max,
  45. children: [
  46. Row(
  47. mainAxisSize: MainAxisSize.max,
  48. children: [
  49. TextButton(
  50. onPressed: () {
  51. logSink.clear();
  52. },
  53. child: const Text(
  54. 'Clear log',
  55. style: TextStyle(color: Colors.white),
  56. ),
  57. ),
  58. Expanded(
  59. child: Align(
  60. alignment: Alignment.topRight,
  61. child: IconButton(
  62. color: Colors.transparent,
  63. onPressed: () {
  64. _removeOverlay();
  65. _isOverlayShowed = !_isOverlayShowed;
  66. },
  67. icon: const Icon(
  68. Icons.close,
  69. color: Colors.white,
  70. )),
  71. ),
  72. ),
  73. ],
  74. ),
  75. const Expanded(
  76. child: SingleChildScrollView(
  77. child: LogWidget(),
  78. ),
  79. )
  80. ],
  81. ),
  82. ),
  83. ),
  84. ),
  85. );
  86. });
  87. Overlay.of(context).insert(_overlayEntry!);
  88. }
  89. _isOverlayShowed = !_isOverlayShowed;
  90. // setState(() {
  91. // });
  92. },
  93. child: const Text(
  94. 'Log',
  95. style: TextStyle(color: Colors.white),
  96. ));
  97. }
  98. }
  99. /// LogWidget
  100. class LogWidget extends StatefulWidget {
  101. /// Construct the [LogWidget]
  102. const LogWidget({
  103. Key? key,
  104. this.logSink,
  105. this.textStyle = const TextStyle(fontSize: 15, color: Colors.white),
  106. }) : super(key: key);
  107. /// This [LogSink] is used to add log.
  108. final LogSink? logSink;
  109. /// The text style of the log.
  110. final TextStyle textStyle;
  111. @override
  112. _LogWidgetState createState() => _LogWidgetState();
  113. }
  114. class _LogWidgetState extends State<LogWidget> {
  115. VoidCallback? _listener;
  116. late final LogSink _logSink;
  117. @override
  118. void initState() {
  119. super.initState();
  120. _logSink = widget.logSink ?? _defaultLogSink;
  121. _listener ??= () {
  122. setState(() {});
  123. };
  124. _logSink.addListener(_listener!);
  125. }
  126. @override
  127. void dispose() {
  128. if (_listener != null) {
  129. _logSink.removeListener(_listener!);
  130. _listener = null;
  131. }
  132. super.dispose();
  133. }
  134. @override
  135. Widget build(BuildContext context) {
  136. return Text(
  137. _logSink.output(),
  138. style: widget.textStyle,
  139. );
  140. }
  141. }
  142. /// Class that add and update the log in [LogActionWidget]
  143. class LogSink extends ChangeNotifier {
  144. final StringBuffer _stringBuffer = StringBuffer();
  145. /// Add log to [LogActionWidget]
  146. void log(String log) {
  147. _stringBuffer.writeln(log);
  148. notifyListeners();
  149. }
  150. /// Get all logs
  151. String output() {
  152. return _stringBuffer.toString();
  153. }
  154. /// Clear logs
  155. void clear() {
  156. _stringBuffer.clear();
  157. notifyListeners();
  158. }
  159. }
  160. final LogSink _defaultLogSink = LogSink();
  161. /// The global [LogSink]
  162. LogSink get logSink => _defaultLogSink;