Home > Mobile >  How can I create a random color Linear Gradient so that the random color generated is constant for e
How can I create a random color Linear Gradient so that the random color generated is constant for e

Time:01-24

body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              decoration: const BoxDecoration(
                shape: BoxShape.circle,
                gradient: LinearGradient(
                  begin: Alignment.bottomCenter,
                  end: Alignment.topRight,
                  colors: [
                    Colors.orange,
                    Colors.red,
                  ],
                ),
              ),
              child: const CircleAvatar(
                radius: 70,
                backgroundColor: Colors.transparent,
                child: Text(
                  "S",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 25.0,
                  ),
                ),
              ),
            )
          ],
        ),

I want the profile color created for each user to be produced only once and remain constant.

body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              decoration: const BoxDecoration(
                shape: BoxShape.circle,
                gradient: LinearGradient(
                  begin: Alignment.bottomCenter,
                  end: Alignment.topRight,
                  colors: [
                    Colors.orange,
                    Colors.red,
                  ],
                ),
              ),
              child: const CircleAvatar(
                radius: 70,
                backgroundColor: Colors.transparent,
                child: Text(
                  "S",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 25.0,
                  ),
                ),
              ),
            )
          ],
        ),

CodePudding user response:

This will generate linear gradient which will be unique for each initial

LinearGradient getGredient(String inital){
      List<Color> colors = [
        Colors.red,
        Colors.blue,
        Colors.green,
        Colors.purple,
        Colors.orange,
        Colors.pink,
        Colors.yellow,
        Colors.teal,
        Colors.indigo,
        Colors.brown,
        Colors.cyan,
        Colors.lime,
        Colors.lightBlue,
        Colors.lightGreen,
        Colors.deepOrange,
        Colors.deepPurple,
        Colors.amber,
        Colors.blueGrey,
        Colors.brown,
        Colors.cyan,
        Colors.deepOrange,
        Colors.deepPurple,
        Colors.green,
        Colors.indigo,
        Colors.lightBlue,
        Colors.lightGreen,
        Colors.lime,
        Colors.orange,
        Colors.pink,
        Colors.purple,
        Colors.red,
        Colors.teal,
        Colors.yellow,
      ];
      int index = inital.codeUnitAt(0) % colors.length;
      return LinearGradient(
        colors: [colors[index], colors[index   1]],
        begin: Alignment.topLeft,
        end: Alignment.bottomRight,
      );
    }

CodePudding user response:

Simply Try this:

...
import 'dart:math' as math;
...
var randomColor = Color((math.Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0);
...
Container(
              decoration: const BoxDecoration(
                shape: BoxShape.circle,
                gradient: LinearGradient(
                  begin: Alignment.bottomCenter,
                  end: Alignment.topRight,
                  colors: [
                    randomColor,
                    randomColor
                  ],
                ),
              ),
  • Related