Created
August 13, 2020 20:17
-
-
Save Medvedoc/c9e2914985e4251be5646821090cee01 to your computer and use it in GitHub Desktop.
My_tasks_Flutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
import 'dart:math' as math; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: ChangeNotifierProvider( | |
create: (_) => ProviderColor(), child: Home())); | |
} | |
} | |
class Home extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
ProviderColor providerColor = Provider.of<ProviderColor>(context); | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Colors.black, | |
centerTitle: true, | |
title: | |
Text('Home Provider', style: TextStyle(color: providerColor.color)), | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
AnimatedContainer( | |
duration: Duration(milliseconds: 500), | |
color: providerColor.color, | |
width: 300, | |
height: 300, | |
), | |
Switch( | |
onChanged: (bool value) { | |
providerColor.valueSwitch(); | |
}, | |
value: providerColor.switcher, | |
) | |
], | |
), | |
), | |
); | |
} | |
} | |
class ProviderColor extends ChangeNotifier { | |
bool switcher = false; | |
Color color; | |
ProviderColor() {this.color = randomColor();} | |
void valueSwitch() { | |
if (switcher == false) {switcher = true;} | |
else {switcher = false;} | |
notifyListeners(); | |
color = randomColor(); | |
} | |
Color randomColor() => Color(math.Random().nextInt(0xffffffff)).withOpacity(0.5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment