google-site-verification=Ob4bKoeqSfwCxusijmOIG1yFGPBhdxoJWBwcZu9KXRk

Counter Number App with Random Color Change App in Flutter

Output Screen

Source Code


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

void main() {
   
   runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  List<Color> colors = [Colors.blue, Colors.amber, Colors.pink];

  int index=0;
  Color colorrightnow;
  void changecolor() 
  {
    setState(() {
      index=Random().nextInt(colors.length); 
      colorrightnow=colors[index];
    });
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Change Color",
      home: Scaffold(
        appBar: AppBar(
          title: Text("Change Color"),
        ),
        backgroundColor: colorrightnow,
        body:Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              SizedBox(height: 20.0),
                RaisedButton(
                  splashColor: Colors.green,
                  color: Colors.red,
                  onPressed: changecolor,
                  child: Text("Click Here",
                  style: TextStyle(
                    fontSize: 20.0,
                    color: Colors.white,
                  ),
                  ),
                )
            ],
          ),  
        ),
      ),
    );
  }
}

No comments:

Post a Comment

Pages