-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new YaruExpandable widget (#140)
Co-authored-by: Feichtmeier <Feichtmeier@users.noreply.github.com>
- Loading branch information
1 parent
ac2386c
commit 1b0bfcc
Showing
3 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
const _kAnimationDuration = Duration(milliseconds: 250); | ||
const _kAnimationCurve = Curves.easeInOutCubic; | ||
|
||
class YaruExpandable extends StatefulWidget { | ||
const YaruExpandable({ | ||
Key? key, | ||
required this.header, | ||
this.expandIcon, | ||
required this.child, | ||
this.collapsedChild, | ||
}) : super(key: key); | ||
|
||
/// Widget placed in the header, against the expand button | ||
final Widget header; | ||
|
||
/// Icon used in the expand button | ||
/// Prefer use a "right arrow" icon | ||
/// A 25° rotation is used when expanded | ||
final Widget? expandIcon; | ||
|
||
/// Widget show when expanded | ||
final Widget child; | ||
|
||
/// Widget show when collapsed | ||
final Widget? collapsedChild; | ||
|
||
@override | ||
State<YaruExpandable> createState() => _YaruExpandableState(); | ||
} | ||
|
||
class _YaruExpandableState extends State<YaruExpandable> { | ||
bool isExpanded = false; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Column( | ||
children: [ | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.spaceBetween, | ||
children: [ | ||
GestureDetector( | ||
onTap: () => setState(() => isExpanded = !isExpanded), | ||
child: widget.header), | ||
IconButton( | ||
splashRadius: 20, | ||
onPressed: () => setState(() => isExpanded = !isExpanded), | ||
icon: AnimatedRotation( | ||
turns: isExpanded ? .25 : 0, | ||
duration: _kAnimationDuration, | ||
curve: _kAnimationCurve, | ||
child: widget.expandIcon ?? const Icon(Icons.arrow_right))), | ||
], | ||
), | ||
AnimatedCrossFade( | ||
firstChild: widget.child, | ||
secondChild: widget.collapsedChild ?? Container(), | ||
crossFadeState: isExpanded | ||
? CrossFadeState.showFirst | ||
: CrossFadeState.showSecond, | ||
sizeCurve: _kAnimationCurve, | ||
duration: _kAnimationDuration) | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters