Flutter:如何制作随机颜色生成器背景 [英] Flutter: How can I make a Random color generator Background
本文介绍了Flutter:如何制作随机颜色生成器背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
生成随机颜色
Generate random colors
return new RaisedButton(
padding: EdgeInsets.symmetric(vertical: 30.0),
color: Colors.primaries random List <blue,green>,
推荐答案
您可以使用 Random
类进行操作:
You can use Random
class to do that:
但是,如果要在按下按钮时更改颜色,则必须使用 StatefulWidget
。简单的例子如下:
But if you want to change the color when button is pressed, you have to use a StatefulWidget
. A simple example is like below:
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],
),
);
}
}
此外,还有一个名为 random_pk 由 pawankumar ,每次调用您应用的构建方法时,都会为我们提供随机颜色。
Also, there is a package called random_pk by pawankumar, that will give us random color each and every time your app's build method get called.
这篇关于Flutter:如何制作随机颜色生成器背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文