Skip to content

[animated_collection] ✨ Add AnimatedVisibility.builder #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/animated_collection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## NEXT

- Adds `AnimatedVisibility.builder`.

## 1.2.1

- Fix memory leaks in `AnimatedLocation`.
Expand Down
2 changes: 2 additions & 0 deletions packages/animated_collection/example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/

# IntelliJ related
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
enableGPUValidationMode = "1"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import Cocoa
import FlutterMacOS

@NSApplicationMain
@main
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}

override func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,74 @@ import 'package:flutter/material.dart';

import 'animated_size_transition.dart';

/// Displays the child when [visible] is `true` and a [SizedBox.shrink] when
/// [visible] is `false`, but does it with a size transition.
/// Conditionally renders the child with an animated size transition when
/// [visible] is `true`; otherwise renders a [SizedBox.shrink].
class AnimatedVisibility extends StatelessWidget {
/// Displays the child when [visible] is `true` and a [SizedBox.shrink] when
/// [visible] is `false`, but does it with a size transition.
/// Conditionally renders the child with an animated size transition when
/// [visible] is `true`; otherwise renders a [SizedBox.shrink].
const AnimatedVisibility({
required this.visible,
required this.child,
this.child,
this.axis = Axis.vertical,
this.axisAlignment = 0,
this.curve = Curves.easeInOut,
this.duration = defaultDuration,
super.key,
});
}) : builder = null;

/// Shows the widget returned by [builder] when [visible] is `true`, and a
/// [SizedBox.shrink] when [visible] is `false`, with a smooth size
/// transition.
///
/// This constructor is helpful when the widget relies on data that’s only
/// available when [visible] is `true`.
///
/// For instance, the following example would throw an error:
///
/// ```dart
/// String? text;
/// AnimatedVisibility(
/// visible: text != null,
/// child: Text(text!), // Throws if text is null
/// )
/// ```
///
/// To prevent that, you’d need to handle the null case explicitly:
///
/// ```dart
/// String? text;
/// AnimatedVisibility(
/// visible: text != null,
/// child: Text(text ?? ''),
/// )
/// ```
///
/// With [AnimatedVisibility.builder], you can safely access the non-null data
/// inside the builder without extra null checks:
///
/// ```dart
/// String? text;
/// AnimatedVisibility.builder(
/// visible: text != null,
/// builder: (context) => Text(text!),
/// )
/// ```

const AnimatedVisibility.builder({
required this.visible,
this.builder,
this.axis = Axis.vertical,
this.axisAlignment = 0,
this.curve = Curves.easeInOut,
this.duration = defaultDuration,
super.key,
}) : child = null;

/// {@macro flutter.widgets.ProxyWidget.child}
final Widget child;
final Widget? child;

/// Called to obtain the child widget.
final WidgetBuilder? builder;

/// If `true`, it displays the [child], else if displays a [SizedBox.shrink].
final bool visible;
Expand All @@ -44,7 +95,7 @@ class AnimatedVisibility extends StatelessWidget {
curve: curve,
fixedCrossAxisSizeFactor: 1,
duration: duration,
child: visible ? child : const SizedBox.shrink(),
child: visible ? child ?? builder!(context) : const SizedBox.shrink(),
);
}
}
Loading