Created
October 24, 2024 10:37
-
-
Save martynov-alex/9eda7065c14bb1606e32f8882cea90d1 to your computer and use it in GitHub Desktop.
Resolve animation
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
| // Copyright 2019 the Dart project authors. 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'; | |
| import 'package:flutter/foundation.dart'; | |
| void main() => runApp(const MyApp()); | |
| class MyApp extends StatelessWidget { | |
| const MyApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| debugShowCheckedModeBanner: false, | |
| theme: ThemeData( | |
| colorSchemeSeed: Colors.blue, | |
| ), | |
| home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| final String title; | |
| const MyHomePage({ | |
| super.key, | |
| required this.title, | |
| }); | |
| @override | |
| State<MyHomePage> createState() => _MyHomePageState(); | |
| } | |
| bool isLow = false; | |
| class _MyHomePageState extends State<MyHomePage> | |
| with SingleTickerProviderStateMixin { | |
| int _counter = 0; | |
| late final _controller = AnimationController( | |
| vsync: this, | |
| duration: const Duration(seconds: 1), | |
| ); | |
| late final _offset = Tween<Offset>( | |
| begin: Offset.zero, | |
| end: const Offset(0, 20), | |
| ).chain(CurveTween(curve: Curves.easeInOut)).animate(_controller); | |
| @override | |
| void initState() { | |
| super.initState(); | |
| PerformanceService.resolveAnimation(context, () => _controller.forward()); | |
| } | |
| @override | |
| void dispose() { | |
| _controller.dispose(); | |
| super.dispose(); | |
| } | |
| void _incrementCounter() { | |
| setState(() { | |
| _counter++; | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| ), | |
| body: Center( | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| const Text( | |
| 'You have pushed the button this many times:', | |
| ), | |
| AnimatedBuilder( | |
| animation: _offset, | |
| builder: (context, child) { | |
| return Transform.translate( | |
| offset: _offset.value, | |
| child: child, | |
| ); | |
| }, | |
| child: Text( | |
| '$_counter', | |
| style: Theme.of(context).textTheme.headlineMedium, | |
| ), | |
| ), | |
| ], | |
| ), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: _incrementCounter, | |
| tooltip: 'Increment', | |
| child: const Icon(Icons.add), | |
| ), | |
| ); | |
| } | |
| } | |
| class PerformanceService { | |
| static void resolveAnimation( | |
| BuildContext context, | |
| AsyncCallback callback, | |
| ) { | |
| WidgetsBinding.instance.addPostFrameCallback((_) async { | |
| final isLowPerformanceDevice = | |
| await Future.delayed(Duration(seconds: 1), () => isLow); | |
| if (!isLowPerformanceDevice && context.mounted) callback.call(); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment