Last active
May 18, 2020 12:52
-
-
Save sergeykondr/34d204d524f4e8791d7411eea98e4bc8 to your computer and use it in GitHub Desktop.
This file contains 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( | |
ChangeNotifierProvider( | |
create: (_) => ColorChangeNotifier(), | |
child: MyApp(), | |
), | |
); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
ColorChangeNotifier _state = Provider.of<ColorChangeNotifier>(context); | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text( | |
'Provider - Homework', | |
style: TextStyle(color: _state._color), | |
), | |
centerTitle: true, | |
backgroundColor: Colors.black, | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
AnimatedContainer( | |
duration: Duration(milliseconds: 400), | |
color: _state._color, | |
width: 200, | |
height: 200, | |
), | |
Switch( | |
value: _state._valueChecked, | |
onChanged: (isChecked) { | |
_state.rndColor(isChecked); | |
}, | |
) | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class ColorChangeNotifier extends ChangeNotifier { | |
bool _valueChecked = false; | |
Color _color = Colors.green; | |
void rndColor(isChecked) { | |
_valueChecked = isChecked; | |
_color = Color((math.Random().nextDouble() * 0xFFFFFF).toInt()).withOpacity(1.0); | |
notifyListeners(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment