Flutter: random color

Posted ajanuw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter: random color相关的知识,希望对你有一定的参考价值。

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

void main() => runApp(App());

class App extends StatefulWidget 
  @override
  _AppState createState() => _AppState();


class _AppState extends State<App> 
  Color color = Colors.red;
  @override
  Widget build(BuildContext context) 
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Container(
                width: 200,
                height: 200,
                color: color,
              ),
              RaisedButton(
                onPressed: () 
                  setState(() 
                    color = randomColor();
                  );
                ,
                child: Text('Random color'),
              ),
            ],
          ),
        ),
      ),
    );
  

  int random(int min, int max) 
    final _random = math.Random();
    return min + _random.nextInt(max - min + 1);
  

  Color randomColor() 
    return Color.fromARGB(
        random(150, 255), random(0, 255), random(0, 255), random(0, 255));
  

以上是关于Flutter: random color的主要内容,如果未能解决你的问题,请参考以下文章

Flutter 取随机数

Flutter / Dart中Firestore流的随机/随机顺序?

flutter创建可移动的stack小部件

dart使用技巧集合

2021-06-29

random模块详解