How to call print() with colorful text to android studio console in flutter
You need to print escape sequences to get the color effects in terminal output.
See also https://en.wikipedia.org/wiki/ANSI_escape_code
https://pub.dartlang.org/packages/ansicolor is a Dart package that makes this easy.
Although this didn't work in Android studio, it is supported in VS Code:
void main() { print('This is a normal message.'); printWarning('This is a warning.'); printError('This is an error.');}void printWarning(String text) { print('\x1B[33m$text\x1B[0m');}void printError(String text) { print('\x1B[31m$text\x1B[0m');}
ANSI escape code explanation
The ANSI escape code string is pretty confusing if you're not familiar with the format.
Here is the string to turn Hello
red:
\x1B[31mHello\x1B[0m
And here it is again with spaces added for clarity between the parts:
\x1B [31m Hello \x1B [0m
Meaning:
\x1B
: ANSI escape sequence starting marker[31m
: Escape sequence for red[0m
: Escape sequence for reset (stop making the text red)
Here are the other colors:
Black: \x1B[30mRed: \x1B[31mGreen: \x1B[32mYellow: \x1B[33mBlue: \x1B[34mMagenta: \x1B[35mCyan: \x1B[36mWhite: \x1B[37mReset: \x1B[0m
Learn more from these links:
I recommend using Grep Console if you don't want to change the way you actually print the characters
You can add a tag like [DEBUG]
to your logs and grep console would do the magic for you.