09-Module 9
09-Module 9
Mobile Programming
MODULE 9:
Animation and TTS&STT
Animation and Motion widgets
• AnimatedAlign • AnimatedPositioned
• AnimatedBuilder • AnimatedSize
• AnimatedContainer • AnimatedWidget
• AnimatedCrossFade • ImplicitlyAnimatedWidget
• AnimatedDefaultTextStyle • DecoratedBoxTransition
• AnimatedList • Hero
• PositionedTransition
• AnimatedListState
• RotationTransition
• AnimatedModalBarrier
• ScaleTransition
• AnimatedOpacity • SizeTransition
• AnimatedPhysicalModel • SlideTransition
https://docs.flutter.dev/ui/widgets/animation
AnimatedAlign
Animated version of Align which automatically transitions the child's position
over a given duration whenever the given alignment changes.
https://api.flutter.dev/flutter/widgets/AnimatedAlign-
class.html?_gl=1*176mj7c*_ga*MTEyNDcwNDgxNS4xNzIwNDA4NjI2*_ga_0
4YGWK0175*MTcyMzM5ODM4NC4xNS4xLjE3MjMzOTk4MjkuMC4wLjA.
import 'package:flutter/material.dart'; AnimatedAlign - Example
/// Flutter code sample for [AnimatedAlign].
https://docs.flutter.dev/ui/widgets/animation
import 'dart:math' as math; AnimatedBuilder - Example
import 'package:flutter/material.dart';
/// AnimationControllers can be created with `vsync: this` because of
/// Flutter code sample for [AnimatedBuilder].
/// TickerProviderStateMixin.
class _AnimatedBuilderExampleState extends State<AnimatedBuilderExample>
void main() => runApp(const
with TickerProviderStateMixin {
AnimatedBuilderExampleApp());
late final AnimationController _controller = AnimationController(
duration: const Duration(seconds: 10),
class AnimatedBuilderExampleApp extends
vsync: this,
StatelessWidget {
)..repeat();
const AnimatedBuilderExampleApp({super.key});
@override
@override
void dispose() {
Widget build(BuildContext context) {
_controller.dispose();
return const MaterialApp(
super.dispose();
home: AnimatedBuilderExample(),
}
);
}
@override
}
Widget build(BuildContext context) {
return AnimatedBuilder(
class AnimatedBuilderExample extends StatefulWidget {
animation: _controller,
const AnimatedBuilderExample({super.key});
child: Container(
width: 200.0,
@override
height: 200.0,
State<AnimatedBuilderExample> createState() =>
color: Colors.green,
_AnimatedBuilderExampleState();
child: const Center(
}
child: Text('Whee!'),
),
),
builder: (BuildContext context, Widget? child) {
return Transform.rotate(
angle: _controller.value * 2.0 * math.pi,
child: child,
);
},
https://api.flutter.dev/flutter/widgets/AnimatedBuilder-
);
class.html?_gl=1*16cdspi*_ga*MTEyNDcwNDgxNS4xNzIwNDA4NjI2*_ga_0
} 4YGWK0175*MTcyMzM5ODM4NC4xNS4xLjE3MjM0MDA5NDMuMC4wLjA.
}
Animation and Motion widgets
• AnimatedAlign • AnimatedPositioned
• AnimatedBuilder • AnimatedSize
• AnimatedContainer • AnimatedWidget
• AnimatedCrossFade • ImplicitlyAnimatedWidget
• AnimatedDefaultTextStyle • DecoratedBoxTransition
• AnimatedList • Hero
• PositionedTransition
• AnimatedListState
• RotationTransition
• AnimatedModalBarrier
• ScaleTransition
• AnimatedOpacity • SizeTransition
• AnimatedPhysicalModel • SlideTransition
https://docs.flutter.dev/ui/widgets/animation
import 'package:flutter/material.dart'; AnimatedContainer - Example
/// Flutter code sample for [AnimatedContainer].
https://api.flutter.dev/flutter/widgets/AnimatedContainer-class.html
Animation and Motion widgets
• AnimatedAlign • AnimatedPositioned
• AnimatedBuilder • AnimatedSize
• AnimatedContainer • AnimatedWidget
• AnimatedCrossFade • ImplicitlyAnimatedWidget
• AnimatedDefaultTextStyle • DecoratedBoxTransition
• AnimatedList • Hero
• PositionedTransition
• AnimatedListState
• RotationTransition
• AnimatedModalBarrier
• ScaleTransition
• AnimatedOpacity • SizeTransition
• AnimatedPhysicalModel • SlideTransition
https://docs.flutter.dev/ui/widgets/animation
AnimatedCrossFade - Example
A widget that cross-fades between two given
children and animates itself between their sizes.
This code fades between two representations of the Flutter logo. It depends on a
boolean field _first; when _first is true, the first logo is shown, otherwise the second
logo is shown. When the field changes state, the AnimatedCrossFade widget cross-
fades between the two forms of the logo over three seconds.
AnimatedCrossFade(
duration: const Duration(seconds: 3),
firstChild: const FlutterLogo(style: FlutterLogoStyle.horizontal, size: 100.0),
secondChild: const FlutterLogo(style: FlutterLogoStyle.stacked, size: 100.0),
crossFadeState: _first ? CrossFadeState.showFirst : CrossFadeState.showSecond,
)
https://api.flutter.dev/flutter/widgets/AnimatedCrossFade-
class.html?_gl=1*1ws87vt*_ga*MTEyNDcwNDgxNS4xNzIwNDA4NjI2*_ga_0
4YGWK0175*MTcyMzM5ODM4NC4xNS4xLjE3MjM0MDEzNjYuMC4wLjA.
Animation and Motion widgets
• AnimatedAlign • AnimatedPositioned
• AnimatedBuilder • AnimatedSize
• AnimatedContainer • AnimatedWidget
• AnimatedCrossFade • ImplicitlyAnimatedWidget
• AnimatedDefaultTextStyle • DecoratedBoxTransition
• AnimatedList • Hero
• PositionedTransition
• AnimatedListState
• RotationTransition
• AnimatedModalBarrier
• ScaleTransition
• AnimatedOpacity • SizeTransition
• AnimatedPhysicalModel • SlideTransition
https://docs.flutter.dev/ui/widgets/animation
AnimatedDefaultTextStyle - Example
https://api.flutter.dev/flutter/widgets/AnimatedDefaultTextStyle-
class.html?_gl=1*1gjsn6o*_ga*MTEyNDcwNDgxNS4xNzIwNDA4NjI2*_ga_0
4YGWK0175*MTcyMzM5ODM4NC4xNS4xLjE3MjM0MDE1NTUuMC4wLjA.
Animation and Motion widgets
• AnimatedAlign • AnimatedPositioned
• AnimatedBuilder • AnimatedSize
• AnimatedContainer • AnimatedWidget
• AnimatedCrossFade • ImplicitlyAnimatedWidget
• AnimatedDefaultTextStyle • DecoratedBoxTransition
• AnimatedList • Hero
• PositionedTransition
• AnimatedListState
• RotationTransition
• AnimatedModalBarrier
• ScaleTransition
• AnimatedOpacity • SizeTransition
• AnimatedPhysicalModel • SlideTransition
https://docs.flutter.dev/ui/widgets/animation
AnimatedList - Example
A scrolling container that animates items when they are inserted or removed.
By default, AnimatedList will automatically pad the limits of the list's scrollable to
avoid partial obstructions indicated by MediaQuery's padding. To avoid this
behavior, override with a zero padding property.
The following example demonstrates how to override the default top and bottom
padding using MediaQuery.removePadding.
https://docs.flutter.dev/ui/widgets/animation
AnimatedModalBarrier
A widget that prevents the user from interacting with widgets behind itself, and can be
configured with an animated color value.
The modal barrier is the scrim that is rendered behind each route, which generally
prevents the user from interacting with the route below the current route, and
normally partially obscures such routes.
For example, when a dialog is on the screen, the page below the dialog is usually
darkened by the modal barrier.
This widget is similar to ModalBarrier except that it takes an animated color instead of a
single color.
Animation and Motion widgets
• AnimatedAlign • AnimatedPositioned
• AnimatedBuilder • AnimatedSize
• AnimatedContainer • AnimatedWidget
• AnimatedCrossFade • ImplicitlyAnimatedWidget
• AnimatedDefaultTextStyle • DecoratedBoxTransition
• AnimatedList • Hero
• PositionedTransition
• AnimatedListState
• RotationTransition
• AnimatedModalBarrier
• ScaleTransition
• AnimatedOpacity • SizeTransition
• AnimatedPhysicalModel • SlideTransition
https://docs.flutter.dev/ui/widgets/animation
AnimatedOpacity
Animated version of Opacity which automatically transitions the child's opacity over a
given duration whenever the given opacity changes. AnimatedOpacity is an implicitly
animated widget.
class LogoFade extends StatefulWidget {
const LogoFade({super.key});
@override
State<LogoFade> createState() => LogoFadeState();
}
void _changeOpacity() {
setState(() => opacityLevel = opacityLevel == 0 ? 1.0 : 0.0);
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedOpacity(
opacity: opacityLevel,
duration: const Duration(seconds: 3),
child: const FlutterLogo(),
),
ElevatedButton(
onPressed: _changeOpacity,
child: const Text('Fade Logo'),
),
],
);
}
}
Animation and Motion widgets
• AnimatedAlign • AnimatedPositioned
• AnimatedBuilder • AnimatedSize
• AnimatedContainer • AnimatedWidget
• AnimatedCrossFade • ImplicitlyAnimatedWidget
• AnimatedDefaultTextStyle • DecoratedBoxTransition
• AnimatedList • Hero
• PositionedTransition
• AnimatedListState
• RotationTransition
• AnimatedModalBarrier
• ScaleTransition
• AnimatedOpacity • SizeTransition
• AnimatedPhysicalModel • SlideTransition
https://docs.flutter.dev/ui/widgets/animation
AnimatedPhysicalModel
The color is animated if the animateColor property is set; otherwise, the color changes
immediately at the start of the animation for the other two properties. This allows the
color to be animated independently (e.g. because it is being driven by an
AnimatedTheme).
https://docs.flutter.dev/ui/widgets/animation
AnimatedPositioned
Animated version of Positioned which
final Duration duration;
automatically transitions the child's position
final Curve curve;
over a given duration whenever the given
@override
position changes. State<AnimatedPositionedExample> createState() =>
_AnimatedPositionedExampleState();
}
import 'package:flutter/material.dart';
class _AnimatedPositionedExampleState extends State<AnimatedPositionedExample> {
/// Flutter code sample for [AnimatedPositioned]. bool selected = false;
23
Animation and Motion widgets
• AnimatedAlign • AnimatedPositioned
• AnimatedBuilder • AnimatedSize
• AnimatedContainer • AnimatedWidget
• AnimatedCrossFade • ImplicitlyAnimatedWidget
• AnimatedDefaultTextStyle • DecoratedBoxTransition
• AnimatedList • Hero
• PositionedTransition
• AnimatedListState
• RotationTransition
• AnimatedModalBarrier
• ScaleTransition
• AnimatedOpacity • SizeTransition
• AnimatedPhysicalModel • SlideTransition
https://docs.flutter.dev/ui/widgets/animation
AnimatedSize
Animated widget that automatically
class AnimatedSizeExample extends StatefulWidget {
transitions its size over a given duration const AnimatedSizeExample({
required this.duration,
whenever the given child's size changes. required this.curve,
super.key,
});
https://docs.flutter.dev/ui/widgets/animation
AnimatedWidget
AnimatedWidget is most commonly used with Animation objects, which are Listenable, but
it can be used with any Listenable, including ChangeNotifier and ValueNotifier.
import 'package:flutter/material.dart';
/// Flutter code sample for [AnimatedWidget]. class AnimatedWidgetExample extends StatefulWidget {
const AnimatedWidgetExample({super.key});
void main() => runApp(const AnimatedWidgetExampleApp());
@override
class AnimatedWidgetExampleApp extends StatelessWidget { State<AnimatedWidgetExample> createState() => _AnimatedWidgetExampleState();
const AnimatedWidgetExampleApp({super.key}); }
https://docs.flutter.dev/ui/widgets/animation
ImplicitlyAnimatedWidget
• Widgets of this type will not animate when they are first added to the
widget tree. Rather, when they are rebuilt with different values, they
will respond to those changes by animating the changes over a
specified duration.
https://docs.flutter.dev/ui/widgets/animation
DecoratedBoxTransition
boxShadow: const <BoxShadow>[
•
BoxShadow(
Animated version of a DecoratedBox that color: Color(0x66666666),
blurRadius: 10.0,
animates the different properties of its spreadRadius: 3.0,
offset: Offset(0, 6.0),
Decoration. ],
),
),
import 'package:flutter/material.dart';
end: BoxDecoration(
color: const Color(0xFFFFFFFF),
/// Flutter code sample for [DecoratedBoxTransition].
border: Border.all(
style: BorderStyle.none,
void main() => runApp(const DecoratedBoxTransitionExampleApp());
),
borderRadius: BorderRadius.zero,
class DecoratedBoxTransitionExampleApp extends StatelessWidget {
// No shadow.
const DecoratedBoxTransitionExampleApp({super.key});
),
);
@override
late final AnimationController _controller = AnimationController(
Widget build(BuildContext context) {
vsync: this,
return const MaterialApp(
duration: const Duration(seconds: 3),
home: DecoratedBoxTransitionExample(),
)..repeat(reverse: true);
);
}
@override
}
void dispose() {
_controller.dispose();
class DecoratedBoxTransitionExample extends StatefulWidget {
super.dispose();
const DecoratedBoxTransitionExample({super.key});
}
@override
@override
State<DecoratedBoxTransitionExample> createState() =>
Widget build(BuildContext context) {
_DecoratedBoxTransitionExampleState();
return ColoredBox(
}
color: Colors.white,
/// [AnimationController]s can be created with `vsync: this` because of
child: Center(
/// [TickerProviderStateMixin].
child: DecoratedBoxTransition(
class _DecoratedBoxTransitionExampleState
decoration: decorationTween.animate(_controller),
extends State<DecoratedBoxTransitionExample> with TickerProviderStateMixin {
child: Container(
final DecorationTween decorationTween = DecorationTween(
width: 200,
begin: BoxDecoration(
height: 200,
color: const Color(0xFFFFFFFF),
padding: const EdgeInsets.all(10),
border: Border.all(style: BorderStyle.none),
child: const FlutterLogo(),
borderRadius: BorderRadius.circular(60.0),
),
),
),
);
}
}
Animation and Motion widgets
• AnimatedAlign • AnimatedPositioned
• AnimatedBuilder • AnimatedSize
• AnimatedContainer • AnimatedWidget
• AnimatedCrossFade • ImplicitlyAnimatedWidget
• AnimatedDefaultTextStyle • DecoratedBoxTransition
• AnimatedList • Hero
• PositionedTransition
• AnimatedListState
• RotationTransition
• AnimatedModalBarrier
• ScaleTransition
• AnimatedOpacity • SizeTransition
• AnimatedPhysicalModel • SlideTransition
https://docs.flutter.dev/ui/widgets/animation
Hero
• A widget that marks its child as being a candidate for hero animations.
• When a PageRoute is pushed or popped with the Navigator, the entire screen's
content is replaced. An old route disappears and a new route appears. If there's
a common visual feature on both routes then it can be helpful for orienting the
user for the feature to physically move from one page to the other during the
routes' transition. Such an animation is called a hero animation. The hero
widgets "fly" in the Navigator's overlay during the transition and while they're
in-flight they're, by default, not shown in their original locations in the old and
new routes.
• To label a widget as such a feature, wrap it in a Hero widget. When navigation
happens, the Hero widgets on each route are identified by the HeroController.
For each pair of Hero widgets that have the same tag, a hero animation is
triggered.
• If a Hero is already in flight when navigation occurs, its flight animation will be
redirected to its new destination. The widget shown in-flight during the
transition is, by default, the destination route's Hero's child.
• For a Hero animation to trigger, the Hero has to exist on the very first frame of
the new page's animation.
• Routes must not contain more than one Hero for each tag.
Animation and Motion widgets
• AnimatedAlign • AnimatedPositioned
• AnimatedBuilder • AnimatedSize
• AnimatedContainer • AnimatedWidget
• AnimatedCrossFade • ImplicitlyAnimatedWidget
• AnimatedDefaultTextStyle • DecoratedBoxTransition
• AnimatedList • Hero
• PositionedTransition
• AnimatedListState
• RotationTransition
• AnimatedModalBarrier
• ScaleTransition
• AnimatedOpacity • SizeTransition
• AnimatedPhysicalModel • SlideTransition
https://docs.flutter.dev/ui/widgets/animation
Transition Animations
With Flutter:
$ flutter pub add flutter_tts
This will add a line like this to your package's pubspec.yaml
(and run an implicit flutter pub get):
dependencies: flutter_tts: ^4.2.0
instantiate FlutterTts
FlutterTts flutterTts = FlutterTts();
await flutterTts.pause()
Speech to Text
speech_to_text