产生随机颜色
return new RaisedButton( padding: EdgeInsets.symmetric(vertical: 30.0), color: Colors.primaries random List <blue,green>,
您可以使用Randomclass来做到这一点:
Random
但是,如果要在按下按钮时更改颜色,则必须使用StatefulWidget。一个简单的例子如下:
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], ), ); } }
此外,还有一个包叫做random_pk通过pawankumar,将每一个你的应用程序的构建方法被调用时给我们随机颜色。