skip to Main Content
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,
                  ),
                ),
              ),
            )
          ],
        ),

2

Answers


  1. 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,
          );
        }
    
    Login or Signup to reply.
  2. 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
                      ],
                    ),
                  ),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search