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),
))
)
);
}
}