颤振:如何制作随机颜色生成器背景

Dev*_* Ed 17 dart flutter

生成随机颜色

return new RaisedButton(


    padding:  EdgeInsets.symmetric(vertical: 30.0),
    color: Colors.primaries random  List <blue,green>,
Run Code Online (Sandbox Code Playgroud)

Akb*_*him 50

Colors类中有一个内置的材料颜色列表。你可以像下面一样使用它

Colors.primaries[Random().nextInt(Colors.primaries.length)]
Run Code Online (Sandbox Code Playgroud)

例如

import 'dart:math';

Icon(
    Icons.account_circle,
    size: 40,
    color: Colors.primaries[Random().nextInt(Colors.primaries.length)],
)
Run Code Online (Sandbox Code Playgroud)

以上是使用随机颜色为您的列表着色的最简单和最快的方法。您不需要维护颜色列表。

  • 我们可以得到使用这个方法生成了哪种颜色吗? (2认同)
  • 是的你可以。生成的随机“int”决定选择哪种颜色。您可以在“局部变量”中使用该随机“int”,并再次使用它来了解您生成的颜色。 (2认同)

小智 21

这是我获得随机颜色的方法:

Color((math.Random().nextDouble() * 0xFFFFFF).toInt() << 0).withOpacity(1.0)
Run Code Online (Sandbox Code Playgroud)

  • 对于初学者:使用此import语句`import'dart:math'作为数学; (5认同)

Bla*_*nka 16

你可以使用Randomclass来做到这一点:

但是如果要在按下按钮时更改颜色,则必须使用a StatefulWidget.一个简单的例子如下:

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

void main() {
  runApp(
    MaterialApp(
      home: MyApp(),
    ),
  );
}

class MyApp extends StatefulWidget {
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  List colors = [Colors.red, Colors.green, Colors.yellow];
  Random random = new Random();

  int index = 0;

  void changeIndex() {
    setState(() => index = random.nextInt(3));
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: RaisedButton(
        onPressed: () => changeIndex(),
        child: Text('Click'),
        color: colors[index],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

此外,pawankumar还有一个名为random_pk软件包,它会在每次调用app的构建方法时为我们提供随机颜色.


use*_*484 14

import 'dart:math';
import 'dart:ui';

class Util {
  static Color randomColor() {
    return Color(Random().nextInt(0xffffffff));
  }
}
Run Code Online (Sandbox Code Playgroud)

对于不透明颜色:

static Color randomOpaqueColor() {
  return Color(Random().nextInt(0xffffffff)).withAlpha(0xff);
}
Run Code Online (Sandbox Code Playgroud)


And*_*rey 8

为了获得随机颜色,我执行以下操作:

import 'dart:math' as math;

final rnd = math.Random();

Color getRandomColor() => Color(rnd.nextInt(0xffffffff));
Run Code Online (Sandbox Code Playgroud)


小智 5

每次调用应用程序的构建方法时,这都会为按钮生成不同的颜色

    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(),
          ),
        );
      }
  }
Run Code Online (Sandbox Code Playgroud)


Bla*_*nka 5

获得大量颜色的另一种方法是使用Random带有Color.fromARGBor 的类Color.fromRGBO

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

void main() {
  runApp(MaterialApp(home: MyPage()));
}

class MyPage extends StatefulWidget {
  @override
  _MyPageState createState() => new _MyPageState();
}

class _MyPageState extends State<MyPage> {
  final Random _random = Random();

  Color _color = Color(0xFFFFFFFF);

  void changeColor() {
    setState(() {
      _color = Color.fromARGB(
        //or with fromRGBO with fourth arg as _random.nextDouble(),
        _random.nextInt(256),
        _random.nextInt(256),
        _random.nextInt(256),
        _random.nextInt(256),
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: InkWell(
        onTap: changeColor,
        child: Container(
          color: _color,
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

如果您使用Color.fromRGBO

第四个参数应在0.01.0和幸运的是我们有_random.nextDouble(),让之间的值0.0,以1.0随机。

顺便一提:

  • R - 红色
  • B - 蓝色
  • G - 绿色
  • O - 不透明度
  • A - 阿尔法


Jei*_*son 5

通过下面这种方式,您可以使用一行命令获得随机颜色,而无需任何导入:

 ([...Colors.primaries]..shuffle()).first
Run Code Online (Sandbox Code Playgroud)

这也是 dart 中级联表示法和扩展运算符的一个很好的例子。在这里您可以找到有关它的更多信息。