Created
August 16, 2023 07:59
-
-
Save pmatatias/bb2ccf9b9dd3399fb124142018299dc9 to your computer and use it in GitHub Desktop.
opacity blinking
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
/// | |
/// pmatatias | |
/// | |
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
const appTitle = 'Opacity Demo'; | |
return const MaterialApp( | |
title: appTitle, | |
home: MyHomePage(title: appTitle), | |
); | |
} | |
} | |
class BlinkingWidget extends AnimatedWidget { | |
const BlinkingWidget({ | |
super.key, | |
required AnimationController controller, | |
this.child, | |
}) : super(listenable: controller); | |
final Widget? child; | |
Animation<double> get _opacity => listenable as Animation<double>; | |
@override | |
Widget build(BuildContext context) { | |
return AnimatedOpacity( | |
// If the widget is visible, animate to 0.0 (invisible). | |
// If the widget is hidden, animate to 1.0 (fully visible). | |
opacity: _opacity.value > 0.5 ? 1.0 : 0.0, | |
duration: const Duration(milliseconds: 1000), | |
// The green box must be a child of the AnimatedOpacity widget. | |
child: child ?? | |
Container( | |
width: 200, | |
height: 200, | |
color: Colors.green, | |
), | |
); | |
} | |
} | |
// The StatefulWidget's job is to take data and create a State class. | |
// In this case, the widget takes a title, and creates a _MyHomePageState. | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({ | |
super.key, | |
required this.title, | |
}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
// The State class is responsible for two things: holding some data you can | |
// update and building the UI using that data. | |
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { | |
late final AnimationController _controller = AnimationController( | |
duration: const Duration(milliseconds: 3000), | |
vsync: this, | |
)..repeat(); | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(20), | |
child: Column(children: [ | |
BlinkingWidget(controller: _controller), | |
SizedBox(height:30), | |
BlinkingWidget( | |
controller: _controller, | |
child: LinearProgressIndicator( | |
value: 730 / 785, | |
minHeight: 30, | |
),), | |
])), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment