Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

android studio - Flutter App: Background doesn't change from black to teal

I created a small application with two dice. It works perfectly like I wanted to do it. For the first time, I have a problem with changing the background color of this page. As you can see in my Code below, I chose the backgroundcolor "teal". I have no idea, why this Background doesn't change to "teal". On all the other pages of my App, the Backgroundcolor is "teal"

Can someone help me with this problem.

Here's the complete code of this page:

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

void main() {
  return runApp(
    MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.teal,
        body: DicePage(),
      ),
    ),
  );
}

class DicePage extends StatefulWidget {
  @override
  _DicePageState createState() => _DicePageState();
}
class _DicePageState extends State<DicePage> {
  int leftDiceNumber = 1;
  int rightDiceNumber = 1;

  void changeDiceFace() {
    setState(() {
      leftDiceNumber = Random().nextInt(6) + 1;
      rightDiceNumber = Random().nextInt(6) + 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(

        children: <Widget>[
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(40.0),
              child: FlatButton(
                onPressed: () {
                  setState(() {
                    leftDiceNumber = Random().nextInt(6) + 1;
                  });
                },
                child: Image.asset('images/dice$leftDiceNumber.png'),
              ),
            ),
          ),

          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(40.0),
              child: FlatButton(
                onPressed: () {
                  changeDiceFace();
                },
                child: Image.asset('images/dice$rightDiceNumber.png'),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
question from:https://stackoverflow.com/questions/65847851/flutter-app-background-doesnt-change-from-black-to-teal

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your MaterialApp there is one method name ThemeData. You can set theme of your app through this. You can follow below code

void main() {
  return runApp(
    MaterialApp(
    theme: ThemeData(
          primarySwatch: Colors.teal,
        ),
      home: Scaffold(
        body: DicePage(),
      ),
    ),
  );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...