Flutter: How can I make a Random color generator B

2020-05-25 03:39发布

Generate random colors

return new RaisedButton(


    padding:  EdgeInsets.symmetric(vertical: 30.0),
    color: Colors.primaries random  List <blue,green>,

标签: dart flutter
9条回答
smile是对你的礼貌
2楼-- · 2020-05-25 04:00

This will generate a different color for the button everytime the build method for the app is called

    import 'package:flutter/material.dart';
    import 'dart:math';

    void main() => runApp(new MyApp());

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
      // TODO: implement build
        return new MaterialApp(
            title: "Raised Button",
            theme: new ThemeData(
               primarySwatch: Colors.teal,
            ),
            home: new HomePage());
      }
    }

    class HomePage extends StatefulWidget {
      @override
      HomePageState createState() => new HomePageState();
    }

    class HomePageState extends State<HomePage> {
      //contains the colours for the circle Avatars
      final List<Color> circleColors = [Colors.red, Colors.blue, Colors.green];

      Color randomGenerator() {
        return circleColors[new Random().nextInt(2)];
      }

      @override
      Widget build(BuildContext context) {
        return Center(
          child: RaisedButton(
            onPressed: () => {},
            child: Text('Click'),
            color: randomGenerator(),
          ),
        );
      }
  }
查看更多
小情绪 Triste *
3楼-- · 2020-05-25 04:01

There is an in-built list of material colors in the Colors class. You can use it like below

Colors.primaries[Random().nextInt(Colors.primaries.length)]

e.g

import 'dart:math';

Icon(
    Icons.account_circle,
    size: 40,
    color: Colors.primaries[Random().nextInt(Colors.primaries.length)],
    )

Above is the simplest and fastest way to colorize your list with random colors. You don't need to maintain a list of colors.

查看更多
孤傲高冷的网名
4楼-- · 2020-05-25 04:02

To get random color I do the next:

import 'dart:math' as math;

final rnd = math.Random();

Color getRandomColor() => Color(rnd.nextInt(0xffffffff));
查看更多
欢心
5楼-- · 2020-05-25 04:04

Simplest and best suggested way to do this is:

Step 1: Add dependency in pubspec.yaml random_color: ^1.0.3

Step 2: Add import import 'package:random_color/random_color.dart';

Step 3: In "color" attribute write color: RandomColor().randomColor();

查看更多
ゆ 、 Hurt°
6楼-- · 2020-05-25 04:07

This is my way to get a random color:

Color((math.Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0)
查看更多
够拽才男人
7楼-- · 2020-05-25 04:12

We can use random class for that But there is a random_color plugin in the flutter that can help us for generating random color and also we can select high saturation colors with this code:

import 'package:random_color/random_color.dart';

RandomColor _randomColor = RandomColor();

Color _color = _randomColor.randomColor(
  colorSaturation: ColorSaturation.highSaturation
);

And light colors with this code:

import 'package:random_color/random_color.dart';

RandomColor _randomColor = RandomColor();

Color _color = _randomColor.randomColor(
  colorBrightness: ColorBrightness.light
);

For more options read this https://pub.dev/packages/random_color#-readme-tab-

查看更多
登录 后发表回答