mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
I recommend reviewing each commit individually. The following were suppressed instead of migrated to minimize the time the tree is closed: 1. The [`Radio` -> `RadioGroup` migration](https://docs.flutter.dev/release/breaking-changes/radio-api-redesign). Tracked by: https://github.com/flutter/flutter/issues/179088. Part of: https://github.com/flutter/flutter/issues/178827 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
205 lines
6.3 KiB
Dart
205 lines
6.3 KiB
Dart
// Copyright 2014 The Flutter 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';
|
|
|
|
/// An example of [AnimationController] and [SlideTransition].
|
|
|
|
// Occupies the same width as the widest single digit used by AnimatedDigit.
|
|
//
|
|
// By stacking this widget behind AnimatedDigit's visible digit, we
|
|
// ensure that AnimatedWidget's width will not change when its value
|
|
// changes. Typically digits like '8' or '9' are wider than '1'. If
|
|
// an app arranges several AnimatedDigits in a centered Row, we don't
|
|
// want the Row to wiggle when the digits change because the overall
|
|
// width of the Row changes.
|
|
class _PlaceholderDigit extends StatelessWidget {
|
|
const _PlaceholderDigit();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final TextStyle textStyle = Theme.of(
|
|
context,
|
|
).textTheme.displayLarge!.copyWith(fontWeight: FontWeight.w500);
|
|
|
|
final Iterable<Widget> placeholderDigits =
|
|
<int>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].map<Widget>((int n) {
|
|
return Text('$n', style: textStyle);
|
|
});
|
|
|
|
return Opacity(
|
|
opacity: 0,
|
|
child: Stack(children: placeholderDigits.toList()),
|
|
);
|
|
}
|
|
}
|
|
|
|
// Displays a single digit [value].
|
|
//
|
|
// When the value changes the old value slides upwards and out of sight
|
|
// at the same as the new value slides into view.
|
|
class AnimatedDigit extends StatefulWidget {
|
|
const AnimatedDigit({super.key, required this.value});
|
|
|
|
final int value;
|
|
|
|
@override
|
|
State<AnimatedDigit> createState() => _AnimatedDigitState();
|
|
}
|
|
|
|
class _AnimatedDigitState extends State<AnimatedDigit>
|
|
with SingleTickerProviderStateMixin {
|
|
static const Duration defaultDuration = Duration(milliseconds: 300);
|
|
|
|
late final AnimationController controller;
|
|
late int incomingValue;
|
|
late int outgoingValue;
|
|
// widget.value updates that occurred while the animation is underway.
|
|
List<int> pendingValues = <int>[];
|
|
Duration duration = defaultDuration;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
controller = AnimationController(duration: duration, vsync: this);
|
|
controller.addStatusListener(handleAnimationCompleted);
|
|
incomingValue = widget.value;
|
|
outgoingValue = widget.value;
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
void handleAnimationCompleted(AnimationStatus status) {
|
|
if (status.isCompleted) {
|
|
if (pendingValues.isNotEmpty) {
|
|
// Display the next pending value. The duration was scaled down
|
|
// in didUpdateWidget by the total number of pending values so
|
|
// that all of the pending changes are shown within
|
|
// defaultDuration of the last one (the past pending change).
|
|
controller.duration = duration;
|
|
animateValueUpdate(incomingValue, pendingValues.removeAt(0));
|
|
} else {
|
|
controller.duration = defaultDuration;
|
|
}
|
|
}
|
|
}
|
|
|
|
void animateValueUpdate(int outgoing, int incoming) {
|
|
setState(() {
|
|
outgoingValue = outgoing;
|
|
incomingValue = incoming;
|
|
controller.forward(from: 0);
|
|
});
|
|
}
|
|
|
|
// Rebuilding the widget with a new value causes the animations to run.
|
|
// If the widget is updated while the value is being changed the new
|
|
// value is added to pendingValues and is taken care of when the current
|
|
// animation is complete (see handleAnimationCompleted()).
|
|
@override
|
|
void didUpdateWidget(AnimatedDigit oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (widget.value != oldWidget.value) {
|
|
if (controller.isAnimating) {
|
|
// We're in the middle of animating outgoingValue out and
|
|
// incomingValue in. Shorten the duration of the current
|
|
// animation as well as the duration for animations that
|
|
// will show the pending values.
|
|
pendingValues.add(widget.value);
|
|
final double percentRemaining = 1 - controller.value;
|
|
duration =
|
|
defaultDuration * (1 / (percentRemaining + pendingValues.length));
|
|
controller.animateTo(1.0, duration: duration * percentRemaining);
|
|
} else {
|
|
animateValueUpdate(incomingValue, widget.value);
|
|
}
|
|
}
|
|
}
|
|
|
|
// When the controller runs forward both SlideTransitions' children
|
|
// animate upwards. This takes the outgoingValue out of sight and the
|
|
// incoming value into view. See animateValueUpdate().
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final TextStyle textStyle = Theme.of(context).textTheme.displayLarge!;
|
|
return ClipRect(
|
|
child: Stack(
|
|
children: <Widget>[
|
|
const _PlaceholderDigit(),
|
|
SlideTransition(
|
|
position: controller.drive(
|
|
Tween<Offset>(
|
|
begin: Offset.zero,
|
|
end: const Offset(0, -1), // Out of view above the top.
|
|
),
|
|
),
|
|
child: Text(
|
|
key: ValueKey<int>(outgoingValue),
|
|
'$outgoingValue',
|
|
style: textStyle,
|
|
),
|
|
),
|
|
SlideTransition(
|
|
position: controller.drive(
|
|
Tween<Offset>(
|
|
begin: const Offset(0, 1), // Out of view below the bottom.
|
|
end: Offset.zero,
|
|
),
|
|
),
|
|
child: Text(
|
|
key: ValueKey<int>(incomingValue),
|
|
'$incomingValue',
|
|
style: textStyle,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class AnimatedDigitApp extends StatelessWidget {
|
|
const AnimatedDigitApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const MaterialApp(title: 'AnimatedDigit', home: AnimatedDigitHome());
|
|
}
|
|
}
|
|
|
|
class AnimatedDigitHome extends StatefulWidget {
|
|
const AnimatedDigitHome({super.key});
|
|
|
|
@override
|
|
State<AnimatedDigitHome> createState() => _AnimatedDigitHomeState();
|
|
}
|
|
|
|
class _AnimatedDigitHomeState extends State<AnimatedDigitHome> {
|
|
int value = 0;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Center(child: AnimatedDigit(value: value % 10)),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: () {
|
|
setState(() {
|
|
value += 1;
|
|
});
|
|
},
|
|
tooltip: 'Increment Digit',
|
|
child: const Icon(Icons.add),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
runApp(const AnimatedDigitApp());
|
|
}
|