Background Color Changer

 import 'package:flutter/material.dart';

import 'dart:math';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Background Changer',
        debugShowCheckedModeBanner: false,
        theme: ThemeData.dark(),
        home: Scaffold(
          appBar: AppBar(
            title: Text('Random BG'),
          ),
          body: HomePage(),
        ));
  }
}

class HomePage extends StatefulWidget {
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  var color = [
    Colors.orange,
    Colors.red,
    Colors.teal,
    Colors.yellow,
    Colors.green,
    Colors.indigo,
    Colors.pink,
    Colors.brown
  ];

  var curr_color = Colors.white;

  randomColor() {
    var rnd = Random().nextInt(color.length); // generates random number
    setState(() {
      curr_color = color[rnd];
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        color: curr_color,
        child: Center(
            child: RaisedButton(
          color: Colors.orange,
          padding: EdgeInsets.fromLTRB(20.0, 10, 20.0, 10),
          child: Text(
            'Change It',
            style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          ),
          onPressed: randomColor,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
        )));
  }
}



Comments