Created
March 10, 2020 17:16
-
-
Save gspencergoog/d7bfef5df6bd627d9aaf8e817c850dfa to your computer and use it in GitHub Desktop.
InheritedNotifier example
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
| // Flutter code sample for | |
| // This example shows three spinning squares that use the value of the notifier | |
| // on an ancestor [InheritedNotifier] (`SpinModel`) to give them their | |
| // rotation. The notifier doesn't need to know about the children, or need to | |
| // be an animation controller, however. The `SpinModel` class could just as | |
| // easily listen to another object (say, a separate object that keeps the | |
| // value of a slider) that was a [Listenable], and get the value from that. The | |
| // descendants also don't need to have an instance of the [InheritedNotifier] | |
| // in order to use it, they just need to know that there is one in their | |
| // ancestry. This can help with decoupling widgets from their models. | |
| import 'package:flutter/material.dart'; | |
| import 'dart:math' as math; | |
| void main() => runApp(MyApp()); | |
| /// This Widget is the main application widget. | |
| class MyApp extends StatelessWidget { | |
| static const String _title = 'Flutter Code Sample'; | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: _title, | |
| home: Material(child: MyStatefulWidget()), | |
| ); | |
| } | |
| } | |
| class SliderInfo extends ChangeNotifier { | |
| SliderInfo(); | |
| double get value => _value; | |
| double _value = 0.0; | |
| set value(double value) { | |
| if (value != _value) { | |
| _value = value; | |
| notifyListeners(); | |
| } | |
| } | |
| } | |
| class SliderNotifier extends InheritedNotifier<SliderInfo> { | |
| SliderNotifier({ | |
| Key key, | |
| SliderInfo notifier, | |
| Widget child, | |
| }) : super(key: key, notifier: notifier, child: child); | |
| static double of(BuildContext context) { | |
| return context.dependOnInheritedWidgetOfExactType<SliderNotifier>().notifier.value; | |
| } | |
| } | |
| class Spinner extends StatelessWidget { | |
| const Spinner(); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Transform.rotate( | |
| angle: SliderNotifier.of(context) * 2.0 * math.pi, | |
| child: Container( | |
| width: 100, | |
| height: 100, | |
| color: Colors.green, | |
| child: const Center( | |
| child: Text('Whee!'), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| SliderInfo sliderInfo = SliderInfo(); | |
| class MyStatefulWidget extends StatefulWidget { | |
| MyStatefulWidget({Key key}) : super(key: key); | |
| @override | |
| _MyStatefulWidgetState createState() => _MyStatefulWidgetState(); | |
| } | |
| class _MyStatefulWidgetState extends State<MyStatefulWidget> with TickerProviderStateMixin { | |
| AnimationController _controller; | |
| @override | |
| void initState() { | |
| super.initState(); | |
| _controller = AnimationController( | |
| duration: const Duration(seconds: 10), | |
| vsync: this, | |
| )..repeat(); | |
| } | |
| @override | |
| void dispose() { | |
| _controller.dispose(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return SliderNotifier( | |
| notifier: sliderInfo, | |
| child: Builder(builder: (BuildContext context) { | |
| return Column( | |
| children: <Widget>[ | |
| Slider( | |
| min: 0.0, | |
| max: 1.0, | |
| value: SliderNotifier.of(context), | |
| onChanged: (double value) { | |
| sliderInfo.value = value; | |
| }, | |
| ), | |
| Row( | |
| mainAxisAlignment: MainAxisAlignment.spaceAround, | |
| children: const <Widget>[ | |
| Spinner(), | |
| Spinner(), | |
| Spinner(), | |
| ], | |
| ), | |
| ], | |
| ); | |
| }), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment