Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 347 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to call print() with colorful text to android studio console in flutter

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

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

#2
You need to print escape sequences to get the color effects in terminal output.

See also

[To see links please register here]


[To see links please register here]

is a Dart package that makes this easy.
Reply

#3
I recommend using [Grep Console][1] 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.


[1]:

[To see links please register here]

Reply

#4
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.
Reply

#5
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][1]


[1]:
Reply

#6
# 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**.

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

---
### How to open emoji panel?

[**mac os**:](

[To see links please register here]

) <kbd>control</kbd> + <kbd>command</kbd> + <kbd>space</kbd>

[**windows**:](

[To see links please register here]

) <kbd>win</kbd> + <kbd>.</kbd>

[**linux**:](

[To see links please register here]

) <kbd>control</kbd> + <kbd>.</kbd> or <kbd>control</kbd> + <kbd>;</kbd>
Reply

#7
# Styles & Colors in any IDE
![](
)

![](
)

There is a [package](

[To see links please register here]

) that is just as natural as `print()`. Simply call `printRich()`instead.


```dart
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).

Reply

#8
I just wanted to share my **Logger** using the Enhanced Enums.
It works with [Flutter 3][1] and minimum Dart SDK version is `2.17.0`.
```dart
enum Logger {
Black("30"),
Red("31"),
Green("32"),
Yellow("33"),
Blue("34"),
Magenta("35"),
Cyan("36"),
White("37");

final String code;
const Logger(this.code);

void log(dynamic text) =>
print('\x1B[' + code+ 'm' + text.toString() + '\x1B[0m');
}
```

Example:

```dart
Logger.Green.log("I Love Coffee!");
```

[1]:

[To see links please register here]

Reply

#9
import 'dart:developer' as developer;


void printW(String text) {
developer.log('\x1B[33m$text\x1B[0m');
}

void printE(String text) {
developer.log('\x1B[31m$text\x1B[0m');
}

void printI(String text) {
developer.log('\x1B[36m$text\x1B[0m');
}

void printS(String text) {
developer.log('\x1B[32m$text\x1B[0m');
}

Reply

#10


**OUTPUT**

[![enter image description here][1]][1]


[1]:



**USE CASE**

prettyPrint(tag: "Profile", value: "200", type: DebugType.statusCode);
prettyPrint(tag: "Profile", value: "Wrong password entered", type: DebugType.error);
prettyPrint(tag: "Profile", value: "google.com", type: DebugType.url);
prettyPrint(tag: "Profile", value: "key_id", type: DebugType.info);
prettyPrint(tag: "Profile", value: "API response", type: DebugType.response);

**UTILITY**

import 'package:flutter/material.dart';

enum DebugType { error, info, url, response, statusCode }

prettyPrint(
{required String tag,
required dynamic value,
DebugType type = DebugType.info}) {
switch (type) {
case DebugType.statusCode:
{
debugPrint('\x1B[33m${"💎 STATUS CODE $tag: $value"}\x1B[0m');
break;
}
case DebugType.info:
{
debugPrint('\x1B[32m${"⚡ INFO $tag: $value"}\x1B[0m');
break;
}
case DebugType.error:
{
debugPrint('\x1B[31m${"🚨 ERROR $tag: $value"}\x1B[0m');
break;
}
case DebugType.response:
{
debugPrint('\x1B[36m${"💡 RESPONSE $tag: $value"}\x1B[0m');
break;
}
case DebugType.url:
{
debugPrint('\x1B[34m${"📌 URL $tag: $value"}\x1B[0m');
break;
}
}
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through