Last active
July 17, 2024 06:16
-
-
Save Atsumi3/2bcf2a325579519e1db5da5ca7c1d16d to your computer and use it in GitHub Desktop.
[Flutter] 画面最前面にオーバーレイログを表示
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/widgets.dart'; | |
final _loggerKey = GlobalKey(debugLabel: "OverWrapLogger"); | |
class OverWrapLogger extends StatefulWidget { | |
final Widget child; | |
OverWrapLogger({ | |
required this.child, | |
}) : super(key: _loggerKey); | |
static void writeMethodCall(String name) { | |
final state = _loggerKey.currentState; | |
if (state is OverWrapLoggerState) { | |
state._writeMethodCall(name); | |
} | |
} | |
@override | |
State<StatefulWidget> createState() => OverWrapLoggerState(); | |
} | |
class OverWrapLoggerState extends State<OverWrapLogger> { | |
final Map<String, int> _methodCall = {}; | |
String logText = ""; | |
void _writeMethodCall(String name) { | |
final frames = StackTrace.current.toString().split("\n"); | |
setState(() { | |
final name = frames[3] | |
.split(" ") | |
.where((element) => element.contains(".")) | |
.toList()[0]; | |
_methodCall[name] = (_methodCall[name] ?? 0) + 1; | |
logText = _methodCall.entries.map((e) { | |
final number = "${e.value}".padLeft(4, '0'); | |
return "[$number] ${e.key}"; | |
}).join("\n"); | |
}); | |
} | |
Widget _build() { | |
return IgnorePointer( | |
ignoring: true, | |
child: Container( | |
width: double.infinity, | |
height: double.infinity, | |
padding: const EdgeInsets.only( | |
top: 32, | |
left: 8, | |
right: 8, | |
bottom: 6, | |
), | |
color: const Color(0x60000000), | |
child: SingleChildScrollView( | |
scrollDirection: Axis.vertical, | |
child: Text( | |
logText, | |
textDirection: TextDirection.ltr, | |
style: const TextStyle( | |
fontSize: 12, | |
), | |
), | |
), | |
), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Stack( | |
alignment: Alignment.center, | |
fit: StackFit.expand, | |
children: [ | |
widget.child, | |
_build(), | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment