-
-
Save mono0926/4e49173f94749b62d7e9dccee88d824e 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
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
// Mimicking the behavior of VSyncProvider through a global variable | |
TickerProvider vsync; | |
class VSyncProvider extends StatefulWidget { | |
const VSyncProvider({Key key, this.child}) : super(key: key); | |
final Widget child; | |
@override | |
_VSyncProviderState createState() => _VSyncProviderState(); | |
} | |
class _VSyncProviderState extends State<VSyncProvider> | |
with SingleTickerProviderStateMixin { | |
@override | |
Widget build(BuildContext context) { | |
TickerMode.of(context); | |
vsync = this; | |
return widget.child; | |
} | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return VSyncProvider( | |
child: TickerMode( | |
// Voluntarily mute the animation, as a descendant of VSyncProvider | |
enabled: false, | |
child: MaterialApp( | |
home: MyHomePage(), | |
), | |
), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { | |
AnimationController animationController; | |
initState() { | |
super.initState(); | |
animationController = AnimationController( | |
// replace with `this` and the animation would pause | |
vsync: vsync, | |
duration: Duration(seconds: 10), | |
)..repeat(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Example'), | |
), | |
body: Center( | |
child: AnimatedBuilder( | |
animation: animationController, | |
builder: (_, __) { | |
// The animation should not play and stay at 0 | |
return Text((animationController.value * 100).floor().toString()); | |
}, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment