From 46c4fc11642cb806559616e4bc4803fd7f0e393a Mon Sep 17 00:00:00 2001 From: Hari07 Date: Fri, 1 Jul 2022 06:20:05 +0530 Subject: [PATCH] Animated fractionally sized box (#106795) --- .../animated_fractionally_sized_box.0.dart | 68 +++++++++++ packages/flutter/lib/src/widgets/basic.dart | 6 + .../lib/src/widgets/implicit_animations.dart | 107 ++++++++++++++++++ .../widgets/implicit_animations_test.dart | 34 ++++++ 4 files changed, 215 insertions(+) create mode 100644 examples/api/lib/widgets/implicit_animations/animated_fractionally_sized_box.0.dart diff --git a/examples/api/lib/widgets/implicit_animations/animated_fractionally_sized_box.0.dart b/examples/api/lib/widgets/implicit_animations/animated_fractionally_sized_box.0.dart new file mode 100644 index 0000000000..2595cfb67e --- /dev/null +++ b/examples/api/lib/widgets/implicit_animations/animated_fractionally_sized_box.0.dart @@ -0,0 +1,68 @@ +// 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. + +// Flutter code sample for AnimatedFractionallySizedBox + +import 'package:flutter/material.dart'; + +void main() => runApp(const MyApp()); + +class MyApp extends StatelessWidget { + const MyApp({super.key}); + + static const String _title = 'Flutter Code Sample'; + + @override + Widget build(BuildContext context) { + return MaterialApp( + title: _title, + home: Scaffold( + appBar: AppBar(title: const Text(_title)), + body: const MyStatefulWidget(), + ), + ); + } +} + +class MyStatefulWidget extends StatefulWidget { + const MyStatefulWidget({super.key}); + + @override + State createState() => _MyStatefulWidgetState(); +} + +class _MyStatefulWidgetState extends State { + bool selected = false; + + @override + Widget build(BuildContext context) { + return GestureDetector( + onTap: () { + setState(() { + selected = !selected; + }); + }, + child: Center( + child: SizedBox( + width: 200, + height: 200, + child: Container( + color: Colors.red, + child: AnimatedFractionallySizedBox( + widthFactor: selected ? 0.25 : 0.75, + heightFactor: selected ? 0.75 : 0.25, + alignment: selected ? Alignment.topLeft : Alignment.bottomRight, + duration: const Duration(seconds: 1), + curve: Curves.fastOutSlowIn, + child: Container( + color: Colors.blue, + child: const FlutterLogo(size: 75), + ), + ), + ), + ), + ), + ); + } +} diff --git a/packages/flutter/lib/src/widgets/basic.dart b/packages/flutter/lib/src/widgets/basic.dart index 0693e1635b..c14f7caeea 100644 --- a/packages/flutter/lib/src/widgets/basic.dart +++ b/packages/flutter/lib/src/widgets/basic.dart @@ -2798,6 +2798,7 @@ class FractionallySizedBox extends SingleChildRenderObjectWidget { assert(widthFactor == null || widthFactor >= 0.0), assert(heightFactor == null || heightFactor >= 0.0); + /// {@template flutter.widgets.basic.fractionallySizedBox.widthFactor} /// If non-null, the fraction of the incoming width given to the child. /// /// If non-null, the child is given a tight width constraint that is the max @@ -2805,8 +2806,10 @@ class FractionallySizedBox extends SingleChildRenderObjectWidget { /// /// If null, the incoming width constraints are passed to the child /// unmodified. + /// {@endtemplate} final double? widthFactor; + /// {@template flutter.widgets.basic.fractionallySizedBox.heightFactor} /// If non-null, the fraction of the incoming height given to the child. /// /// If non-null, the child is given a tight height constraint that is the max @@ -2814,8 +2817,10 @@ class FractionallySizedBox extends SingleChildRenderObjectWidget { /// /// If null, the incoming height constraints are passed to the child /// unmodified. + /// {@endtemplate} final double? heightFactor; + /// {@template flutter.widgets.basic.fractionallySizedBox.alignment} /// How to align the child. /// /// The x and y values of the alignment control the horizontal and vertical @@ -2834,6 +2839,7 @@ class FractionallySizedBox extends SingleChildRenderObjectWidget { /// specify an [AlignmentGeometry]. /// * [AlignmentDirectional], like [Alignment] for specifying alignments /// relative to text direction. + /// {@endtemplate} final AlignmentGeometry alignment; @override diff --git a/packages/flutter/lib/src/widgets/implicit_animations.dart b/packages/flutter/lib/src/widgets/implicit_animations.dart index 98653ec46d..522ca0590e 100644 --- a/packages/flutter/lib/src/widgets/implicit_animations.dart +++ b/packages/flutter/lib/src/widgets/implicit_animations.dart @@ -2108,3 +2108,110 @@ class _AnimatedPhysicalModelState extends AnimatedWidgetBaseState= 0.0), + assert(heightFactor == null || heightFactor >= 0.0); + + /// The widget below this widget in the tree. + /// + /// {@macro flutter.widgets.ProxyWidget.child} + final Widget? child; + + /// {@macro flutter.widgets.basic.fractionallySizedBox.heightFactor} + final double? heightFactor; + + /// {@macro flutter.widgets.basic.fractionallySizedBox.widthFactor} + final double? widthFactor; + + /// {@macro flutter.widgets.basic.fractionallySizedBox.alignment} + final AlignmentGeometry alignment; + + @override + AnimatedWidgetBaseState createState() => _AnimatedFractionallySizedBoxState(); + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DiagnosticsProperty('alignment', alignment)); + properties.add(DiagnosticsProperty('widthFactor', widthFactor)); + properties.add(DiagnosticsProperty('heightFactor', heightFactor)); + } +} + +class _AnimatedFractionallySizedBoxState extends AnimatedWidgetBaseState { + AlignmentGeometryTween? _alignment; + Tween? _heightFactorTween; + Tween? _widthFactorTween; + + @override + void forEachTween(TweenVisitor visitor) { + _alignment = visitor(_alignment, widget.alignment, (dynamic value) => AlignmentGeometryTween(begin: value as AlignmentGeometry)) as AlignmentGeometryTween?; + if(widget.heightFactor != null) { + _heightFactorTween = visitor(_heightFactorTween, widget.heightFactor, (dynamic value) => Tween(begin: value as double)) as Tween?; + } + if(widget.widthFactor != null) { + _widthFactorTween = visitor(_widthFactorTween, widget.widthFactor, (dynamic value) => Tween(begin: value as double)) as Tween?; + } + } + + @override + Widget build(BuildContext context) { + return FractionallySizedBox( + alignment: _alignment!.evaluate(animation)!, + heightFactor: _heightFactorTween?.evaluate(animation), + widthFactor: _widthFactorTween?.evaluate(animation), + child: widget.child, + ); + } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder description) { + super.debugFillProperties(description); + description.add(DiagnosticsProperty('alignment', _alignment, defaultValue: null)); + description.add(DiagnosticsProperty>('widthFactor', _widthFactorTween, defaultValue: null)); + description.add(DiagnosticsProperty>('heightFactor', _heightFactorTween, defaultValue: null)); + } +} diff --git a/packages/flutter/test/widgets/implicit_animations_test.dart b/packages/flutter/test/widgets/implicit_animations_test.dart index b0b9402cb7..4059687665 100644 --- a/packages/flutter/test/widgets/implicit_animations_test.dart +++ b/packages/flutter/test/widgets/implicit_animations_test.dart @@ -421,6 +421,27 @@ void main() { expect(state.builds, equals(2)); }); + testWidgets('AnimatedFractionallySizedBox onEnd callback test', (WidgetTester tester) async { + await tester.pumpWidget(wrap( + child: TestAnimatedWidget( + callback: mockOnEndFunction.handler, + switchKey: switchKey, + state: _TestAnimatedFractionallySizedBoxWidgetState(), + ), + )); + + final Finder widgetFinder = find.byKey(switchKey); + + await tester.tap(widgetFinder); + await tester.pump(); + expect(mockOnEndFunction.called, 0); + await tester.pump(animationDuration); + expect(mockOnEndFunction.called, 0); + await tester.pump(additionalDelay); + expect(mockOnEndFunction.called, 1); + + await tapTest2and3(tester, widgetFinder, mockOnEndFunction); + }); testWidgets('SliverAnimatedOpacity onEnd callback test', (WidgetTester tester) async { await tester.pumpWidget(TestAnimatedWidget( @@ -812,6 +833,19 @@ class _TestAnimatedOpacityWidgetState extends _TestAnimatedWidgetState { } } +class _TestAnimatedFractionallySizedBoxWidgetState extends _TestAnimatedWidgetState { + @override + Widget getAnimatedWidget() { + return AnimatedFractionallySizedBox( + duration: duration, + onEnd: widget.callback, + heightFactor: toggle ? 0.25 : 0.75, + widthFactor: toggle ? 0.25 : 0.75, + child: child, + ); + } +} + class _TestSliverAnimatedOpacityWidgetState extends _TestAnimatedWidgetState { @override Widget getAnimatedWidget() {