Skip to content
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

YaruExpandable: add optional isExpanded parameter #144

Merged
merged 1 commit into from
Jun 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions example/lib/example_page_items.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ final examplePageItems = <YaruPageItem>[
expandIcon: Icon(YaruIcons.pan_end),
),
YaruExpandable(
isExpanded: true,
collapsedChild: Text(
_lorem,
maxLines: 5,
Expand Down
20 changes: 15 additions & 5 deletions lib/src/yaru_expandable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class YaruExpandable extends StatefulWidget {
this.expandIcon,
required this.child,
this.collapsedChild,
this.isExpanded = false,
}) : super(key: key);

/// Widget placed in the header, against the expand button
Expand All @@ -26,12 +27,21 @@ class YaruExpandable extends StatefulWidget {
/// Widget show when collapsed
final Widget? collapsedChild;

/// Optional initial value.
final bool isExpanded;

@override
State<YaruExpandable> createState() => _YaruExpandableState();
}

class _YaruExpandableState extends State<YaruExpandable> {
bool isExpanded = false;
late bool _isExpanded;

@override
void initState() {
_isExpanded = widget.isExpanded;
super.initState();
}

@override
Widget build(BuildContext context) {
Expand All @@ -41,13 +51,13 @@ class _YaruExpandableState extends State<YaruExpandable> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
onTap: () => setState(() => isExpanded = !isExpanded),
onTap: () => setState(() => _isExpanded = !_isExpanded),
child: widget.header),
IconButton(
splashRadius: 20,
onPressed: () => setState(() => isExpanded = !isExpanded),
onPressed: () => setState(() => _isExpanded = !_isExpanded),
icon: AnimatedRotation(
turns: isExpanded ? .25 : 0,
turns: _isExpanded ? .25 : 0,
duration: _kAnimationDuration,
curve: _kAnimationCurve,
child: widget.expandIcon ?? const Icon(Icons.arrow_right))),
Expand All @@ -56,7 +66,7 @@ class _YaruExpandableState extends State<YaruExpandable> {
AnimatedCrossFade(
firstChild: widget.child,
secondChild: widget.collapsedChild ?? Container(),
crossFadeState: isExpanded
crossFadeState: _isExpanded
? CrossFadeState.showFirst
: CrossFadeState.showSecond,
sizeCurve: _kAnimationCurve,
Expand Down