Generate Random Background Color In Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 10-10-2020

Background color is most important part for every android & iOS application. Because using background color application screen look will set. Basically in most of applications we would see single background color but sometimes app developer wants to change the background color dynamically.

Generate Random Background Color In Flutter Android App

Generate Random Background Color
Complete Code For Generate Random Background Color In Flutter
main.dart

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

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

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
        home: Scaffold(
            body: View()
        )
    );
  }
}

class View extends StatefulWidget {
  ViewState createState() => ViewState();
}

class ViewState extends State{
  Color colorCode = Color(0xFF01579B);
  final Random random = Random();
  generateRandomColor(){
    Color tmpColor = Color.fromARGB(
      random.nextInt(256),
      random.nextInt(256),
      random.nextInt(256),
      random.nextInt(256),
    ) ;

    setState(() {
      colorCode = tmpColor ;
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: colorCode,
        appBar: AppBar(
          backgroundColor: Colors.pink,
            title: Text('Generate Random Background Color'
            )),
        body: Center(
            child: Container(
                margin: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                child:
                RaisedButton(
                  onPressed: () => generateRandomColor(),
                  child: Text('Generate Random Background Color'),
                  textColor: Colors.white,
                  color: Colors.pink,
                  padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
                ))
        )
    );
  }
}

Related Post