How to generate random colors in flutter or dart?

This Article is posted by seven.srikanth at 9/25/2019 5:37:14 PM

In this article, I'm going to share the code required to generate random colors in flutter or dart. Here is the UniqueColorGenerator Class. About the program below, we have created a container which will show random color when the program is run. This program is just to illustrate how this class can be used, but it doesn't do nothing much. Here is the full program for you to try from main.dart file. color: #dcdcaa;](runApp(color: #4ec9b0;](MyApp()); color: #569cd6;](class color: #4ec9b0;](MyApp color: #569cd6;](extends color: #4ec9b0;](StatelessWidget { color: #569cd6;](@override color: #4ec9b0;](Widget color: #dcdcaa;](build(color: #4ec9b0;](BuildContext context) { color: #c586c0;](return color: #4ec9b0;](Row( mainAxisAlignment: color: #4ec9b0;](MainAxisAlignment.center, textDirection: color: #4ec9b0;](TextDirection.ltr, children: [color: #4ec9b0;](Colortile(), color: #4ec9b0;](Colortile()], ); } } color: #569cd6;](class color: #4ec9b0;](Colortile color: #569cd6;](extends color: #4ec9b0;](StatelessWidget { color: #569cd6;](final color: #4ec9b0;](Color myColor = color: #4ec9b0;](UniqueColorGenerator.color: #dcdcaa;](getColor(); color: #569cd6;](@override color: #4ec9b0;](Widget color: #dcdcaa;](build(color: #4ec9b0;](BuildContext context) { color: #c586c0;](return color: #4ec9b0;](Container( width: color: #b5cea8;](100.0, height: color: #b5cea8;](100.0, color: myColor, ); } } color: #569cd6;](class color: #4ec9b0;](UniqueColorGenerator { color: #569cd6;](static color: #4ec9b0;](Random random = color: #c586c0;](new color: #4ec9b0;](Random(); color: #569cd6;](static color: #4ec9b0;](Color color: #dcdcaa;](getColor() { color: #c586c0;](return color: #4ec9b0;](Color.color: #dcdcaa;](fromARGB( color: #b5cea8;](255, random.color: #dcdcaa;](nextInt(color: #b5cea8;](255), random.color: #dcdcaa;](nextInt(color: #b5cea8;](255), random.color: #dcdcaa;](nextInt(color: #b5cea8;](255)); } } ` Thanks, Srikanth

Tags: random colors



2 Comments
Login to comment.
Recent Comments

vitaliylutso69 at 2/19/2020

What if I want to update color by tapping on the widget?

Login to Reply.

seven.srikanth at 3/25/2020

You need to use gestures for that or link an function to ontap property if it has one.





© 2018 - Fluttercentral | Email to me - Seven.srikanth@gmail.com