Files
flutter_server_box/lib/data/provider/debug.dart
2023-09-13 15:22:48 +08:00

61 lines
1.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:toolbox/data/res/ui.dart';
import '../../data/res/misc.dart';
/// format: [NAME][LEVEL]: MESSAGE
final _headReg = RegExp(r'(\[[A-Za-z]+\])(\[[A-Z]+\]): (.*)');
const _level2Color = {
'[INFO]': Colors.blue,
'[WARNING]': Colors.yellow,
};
class DebugProvider extends ChangeNotifier {
final widgets = <Widget>[];
void addText(String text) {
final match = _headReg.allMatches(text);
if (match.isNotEmpty) {
_addWidget(Text.rich(TextSpan(
children: [
TextSpan(
text: match.first.group(1),
style: const TextStyle(color: Colors.cyan),
),
TextSpan(
text: match.first.group(2),
style: TextStyle(color: _level2Color[match.first.group(2)]),
),
TextSpan(
text: '\n${match.first.group(3)}',
)
],
)));
} else {
_addWidget(Text(text));
}
}
void addMultiline(Object data, [Color color = Colors.blue]) {
_addWidget(SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Text('$data', style: TextStyle(color: color)),
));
}
void _addWidget(Widget widget) {
widgets.add(widget);
widgets.add(UIs.height13);
if (widgets.length > Miscs.maxDebugLogLines) {
widgets.removeRange(0, widgets.length - Miscs.maxDebugLogLines);
}
notifyListeners();
}
void clear() {
widgets.clear();
notifyListeners();
}
}