How to call print() with colorful text to android studio console in flutter

Viewed 14497

I need a debug output with colorful string, like node.js chalk.

I tried to find the packages, but there is no proper package.

7 Answers

Although this didn't work in Android studio, it is supported in VS Code:

enter image description here

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.

enter image description here

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[30m
Red:     \x1B[31m
Green:   \x1B[32m
Yellow:  \x1B[33m
Blue:    \x1B[34m
Magenta: \x1B[35m
Cyan:    \x1B[36m
White:   \x1B[37m
Reset:   \x1B[0m

Learn more from these links:

Emoji

You can use colors for text as others mentioned in their answers to have colorful text with a background or foreground color.

But you can use emojis instead! for example, you can use⚠️ for warning messages and for error messages.

Or simply use these notebooks as a color:

: error message
: warning message
: ok status message
: action message
: canceled status message
: Or anything you like and want to recognize immediately by color

Bonus:

This method also helps you to quickly scan and find logs directly in the source code.

But some distributions of Linux’s default emoji font is not colorful by default and you may want to make them colorful, first.


How to open emoji panel?

mac os: control + command + space

windows: win + .

linux: control + . or control + ;

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.

Though text coloring works perfect in terminal it may not work in debug output of IDE (I've tried Idea/Android Studio and VSCode).

Example of ANSI Escape Codes using:

print('\x1B[94m' + "hahAHaHA!!!" + '\x1B[0m');

Examples of using ansicolor package:

import 'package:ansicolor/ansicolor.dart';

main(List<String> arguments) {
  AnsiPen greenPen = AnsiPen()..green();
  AnsiPen greenBackGroundPen = AnsiPen()..green(bg: true);

  AnsiPen redTextBlueBackgroundPen = AnsiPen()..blue(bg: true)..red();

  AnsiPen boldPen = AnsiPen()..white(bold: true);

  AnsiPen someColorPen = AnsiPen()..rgb(r: .5, g: .2, b: .4);

  print(greenPen("Hulk") + " " + greenBackGroundPen("SMASH!!!"));
  print(redTextBlueBackgroundPen("Spider-Man!!!") + " " + boldPen("Far From Home!!!"));

  print(someColorPen("Chameleon"));
}

PS Came here to find some info on text coloring in terminal, not in debug output of IDE. So I decided to write examples here and do not create separate question with answer.

Styles & Colors in any IDE

There is a package that is just as natural as print(). Simply call printRich()instead.

printRich("This is my String", foreground: Colors.blue, italic: true);

printWarning("This is a very important warning"); //Will be yellow

You can change fore- and background colors and adjust text styles (italic, bold, underline, etc.). This package should work with any IDE as long as it uses ANSI (most IDEs can display this, including Android Studio).

Currently, Dart console does not parse ANSI colors in Android Studio or IntelliJ Idea. You can check ANSI color support by calling

import dart.io as io
io.stdout.supportsAnsiEscapes

But you can have colorful logs in Android Studio, just open a terminal window in android studio and call

flutter logs

This command attaches the current terminal session to the active flutter session. Use this terminal for logs. Debug tab, for other functionality.

Preview

Related