生成随机颜色
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)
以上是使用随机颜色为您的列表着色的最简单和最快的方法。您不需要维护颜色列表。
小智 21
这是我获得随机颜色的方法:
Color((math.Random().nextDouble() * 0xFFFFFF).toInt() << 0).withOpacity(1.0)
Run Code Online (Sandbox Code Playgroud)
Bla*_*nka 16
你可以使用Random
class来做到这一点:
但是如果要在按下按钮时更改颜色,则必须使用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)
为了获得随机颜色,我执行以下操作:
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)
获得大量颜色的另一种方法是使用Random
带有Color.fromARGB
or 的类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.0
对1.0
和幸运的是我们有_random.nextDouble()
,让之间的值0.0
,以1.0
随机。
顺便一提:
通过下面这种方式,您可以使用一行命令获得随机颜色,而无需任何导入:
([...Colors.primaries]..shuffle()).first
Run Code Online (Sandbox Code Playgroud)
这也是 dart 中级联表示法和扩展运算符的一个很好的例子。在这里您可以找到有关它的更多信息。
归档时间: |
|
查看次数: |
12108 次 |
最近记录: |