1
0
mirror of https://github.com/flutter/samples.git synced 2026-03-25 22:01:51 +00:00
Files
samples/animations/lib/src/basics/tweens.dart
Brett Morgan 36e7a6ab04 Update for Flutter 3.10 beta (#1746)
## Pre-launch Checklist

- [x] I read the [Flutter Style Guide] _recently_, and have followed its
advice.
- [x] I signed the [CLA].
- [x] I read the [Contributors Guide].
- [x] I updated/added relevant documentation (doc comments with `///`).
- [ ] All existing and new tests are passing.

---------

Co-authored-by: David Iglesias <ditman@gmail.com>
Co-authored-by: Mark Thompson <2554588+MarkTechson@users.noreply.github.com>
Co-authored-by: John Ryan <ryjohn@google.com>
2023-05-11 06:16:31 +10:00

79 lines
2.2 KiB
Dart

// Copyright 2019 The Flutter team. 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';
class TweenDemo extends StatefulWidget {
const TweenDemo({super.key});
static const String routeName = 'basics/tweens';
@override
State<TweenDemo> createState() => _TweenDemoState();
}
class _TweenDemoState extends State<TweenDemo>
with SingleTickerProviderStateMixin {
static const Duration _duration = Duration(seconds: 1);
static const double accountBalance = 1000000;
late final AnimationController controller;
late final Animation<double> animation;
@override
void initState() {
super.initState();
controller = AnimationController(vsync: this, duration: _duration)
..addListener(() {
// Marks the widget tree as dirty
setState(() {});
});
animation = Tween(begin: 0.0, end: accountBalance).animate(controller);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Tweens'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 200),
child: Text('\$${animation.value.toStringAsFixed(2)}',
style: const TextStyle(fontSize: 24)),
),
ElevatedButton(
child: Text(
switch (controller.status) {
AnimationStatus.completed => 'Buy a Mansion',
AnimationStatus.forward => 'Accruing...',
AnimationStatus.reverse => 'Spending...',
_ => 'Win the lottery',
},
),
onPressed: () {
switch (controller.status) {
case AnimationStatus.completed:
controller.reverse();
default:
controller.forward();
}
},
)
],
),
),
);
}
}