yGGq
yGGq

Reputation: 1

Is there a way to change the background color of the body with button click?

I'm fairly new to Flutter and I not able find how to change the background color of the body with a button click.

How it is possible to achieve that?

Upvotes: 0

Views: 306

Answers (1)

tims
tims

Reputation: 530

A little code example of what you have tried would be nice, but this is how i would do it:

class Test extends StatefulWidget {
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      backgroundColor: change_color == true ? Colors.green : Colors.red,
      body: Center(
        child: RaisedButton(
          child: Text('Change Color'),
          onPressed: () {
            setState(() {
              change_color = !change_color;
            });
          },
        ),
      ),
    );
  }

  bool change_color = false;
}

Upvotes: 2

Related Questions