Skip to content

Commit

Permalink
YaruExpandable: add optional isExpanded parameter (#144)
Browse files Browse the repository at this point in the history
Thanks <3
  • Loading branch information
Feichtmeier authored Jun 10, 2022
1 parent 7d7fbc1 commit b4bdfda
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
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

0 comments on commit b4bdfda

Please sign in to comment.