Thursday, September 28, 2023
HomeTECHFlutter change color of text - Flutter Tips

Flutter change color of text – Flutter Tips

To change the color of text in Flutter, you can use the TextStyle widget along with the color property. Here’s an example of how you can change the color of a text widget:

import 'package:flutter/material.dart';

class ColorChangeExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Color Change Example'),
        ),
        body: Center(
          child: Text(
            'Hello, Flutter!',
            style: TextStyle(
              color: Colors.blue, // Change the color here
              fontSize: 24,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(ColorChangeExample());
}

In this example, the TextStyle widget is used to set the properties of the text, including the color property.

You can change the color property to any color value provided by the Colors class in Flutter.

Note that the Colors class provides a set of predefined colors, but you can also define your own custom colors using the Color class.

Remember to import the necessary Flutter packages (flutter/material.dart in this case) for this code to work.

In Flutter, you can change the color of text in various ways. The most common approach is to use the style property of the Text widget to specify the TextStyle, including the color. Here’s how you can do it:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Color Example'),
        ),
        body: Center(
          child: Text(
            'Hello, Flutter!',
            style: TextStyle(
              color: Colors.blue, // Change this to any color you desire
              fontSize: 24,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }
}

Above ex. the text color is set to Colors.blue, but you can replace it with any color you want. You can use named colors from the Colors class or specify colors using hexadecimal values.

For custom colors, you can use Color class constructor with RGBA values:

Color customColor = Color.fromRGBO(255, 0, 127, 1.0); // Reddish pink color

Make sure to use the correct format for specifying colors. For named colors, just use Colors.colorName, and for custom colors, use the Color class with the appropriate constructor.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular