Flutter-如何为StatefulWidget添加背景文本以生成随机颜色

问题描述 投票:0回答:1

刚开始学习Flutter,我尝试用随机颜色生成器创建一个小部件,部分原因是我做到了,因为我不知道如何将背景文本添加到屏幕中央。也许有人可以帮我吗?

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> {
  Random random = new Random();

  Color color ;

  void changeRandomColor() {
    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: changeRandomColor,
            child: Container(
              color: color,
            ),
          )
    );
  }
}
flutter background-color
1个回答
0
投票

您做对了,只需要给Container全屏显示宽度和高度,就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.