Flutter之展开组件

fulutter展开组件的使用
flutter 提供的的 ExpansionTile
ExpansionTile(
                    maintainState: true,
                    tilePadding: EdgeInsets.only(
                      right: MediaQuery.of(context).size.width * 0.5 - 15,
                    ),
                    title: Text("测试"),
                    initiallyExpanded: false,
                    onExpansionChanged: (isOpen) {
                      setState(() {
                        this.showLine = !isOpen;
                      });
                    },
                    children: [
                     Text('数据1'),
                        Text('数据2'),
                           Text('数据3'),
                              Text('数据4'),
                                 Text('数据5'),
                    ],
                  ),
在项目中封装后的组件
/// radius -- card的角度
/// content card中间部分的内容
/// leftTitle 坐标的文字标题
/// showLine -- 标题下是否需要横线
/// titleHeight 标题的高度
/// rightTitle 右上标题
///  showTitle  是否需要标题
/// rightIcon  右边标题旁边的组件
/// rightOnTap 右标题的回调函数
/// isNeedExpand 添加是否展开功能
class CustomCard extends StatefulWidget {
  final double radius;
  final Widget content;
  final String leftTitle;
  final bool showLine;
  final double titleHeight;
  final String rightTitle;
  final bool showTitle;
  final Widget rightIcon;
  final Function onTap;
  final bool isNeedExpand;

  const CustomCard(
      {Key key,
      this.radius,
      this.content,
      this.leftTitle,
      this.showLine = true,
      this.titleHeight,
      this.rightTitle,
      this.showTitle,
      this.rightIcon,
      this.onTap,
      this.isNeedExpand = false})
      : super(key: key);
  @override
  State<StatefulWidget> createState() => _CustomCard(
      this.radius,
      this.content,
      this.leftTitle,
      this.showLine,
      this.titleHeight,
      this.rightTitle,
      this.showTitle,
      this.rightIcon,
      this.onTap,
      this.isNeedExpand);
}

class _CustomCard extends State<CustomCard> {
  final double radius;
  final Widget content;
  final String leftTitle;
  bool showLine;
  final double titleHeight;
  final String rightTitle;
  final bool showTitle;
  final Widget rightIcon;
  final Function onTap;
  bool isNeedExpand;
  _CustomCard(
      this.radius,
      this.content,
      this.leftTitle,
      this.showLine,
      this.titleHeight,
      this.rightTitle,
      this.showTitle,
      this.rightIcon,
      this.onTap,
      this.isNeedExpand);
  @override
  Widget build(BuildContext context) {
    return Card(
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(radius ?? 15)),
      child: Column(
        children: [
          showTitle
              ? Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      alignment: Alignment.center,
                      height: titleHeight ?? 40,
                      padding: EdgeInsets.only(
                        top: 15,
                        left: 15,
                      ),
                      child: InkWell(
                        child: Text(
                          leftTitle ?? '',
                          style: TextStyle(
                            fontSize: 15,
                          ),
                        ),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(
                        right: 15,
                        top: 15,
                      ),
                      child: InkWell(
                        onTap: onTap,
                        child: Row(
                          children: [
                            Container(
                              margin: EdgeInsets.only(right: 2),
                              child: Text(
                                rightTitle ?? '',
                                style: TextStyle(color: CustomColor.loginForm),
                              ),
                            ),
                            rightIcon ?? SizedBox(),
                          ],
                        ),
                      ),
                    )
                  ],
                )
              : SizedBox(
                  height: 0,
                ),
          showLine == true
              ? Container(
                  padding: EdgeInsets.only(left: 15, right: 15, top: 10),
                  child: Line(),
                )
              : Text(''),
          isNeedExpand
              ? Container(
                  child: ExpansionTile(
                    maintainState: true,
                    tilePadding: EdgeInsets.only(
                      right: MediaQuery.of(context).size.width * 0.5 - 15,
                    ),
                    title: Text(""),
                    initiallyExpanded: false,
                    onExpansionChanged: (isOpen) {
                      setState(() {
                        this.showLine = !isOpen;
                      });
                    },
                    children: [
                      content,
                    ],
                  ),
                )
              : content,
        ],
      ),
    );
  }
}
总结

根据项目需要,展开的内容一般是放在Card中的,所以将展开组件封装在Card中,然后跟据属性配置就可以满足Card的内容是否需要展开

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flutter 中可以使用动画库`flutter_animation_set`来实现展开收起动画。具体步骤如下: 1. 导入依赖库:在 `pubspec.yaml` 文件中添加以下依赖库: ``` dependencies: flutter_animation_set: ^1.2.0 ``` 2. 创建动画组件:使用 `AnimationSet` 组件创建动画效果,可以设置动画的类型、方向、持续时间等参数。例如: ```dart AnimationSet( child: Container( height: 200, child: Text('Hello, World!'), ), animations: [ AnimationItem( tween: Tween<double>(begin: 0, end: 1), duration: Duration(milliseconds: 500), delay: Duration(milliseconds: 0), curve: Curves.easeInOut, builder: (context, child, value) => Opacity( opacity: value, child: Transform.translate( offset: Offset(0, 100 - 100 * value), child: child, ), ), ), ], ) ``` 以上代码实现了一个在垂直方向上从上往下展开的动画效果,持续时间为 500 毫秒,使用了 `easeInOut` 曲线。 3. 添加交互事件:使用 `GestureDetector` 组件监听用户的手势事件,触发动画效果。例如: ```dart bool _expanded = false; GestureDetector( onTap: () { setState(() { _expanded = !_expanded; }); }, child: _expanded ? AnimationSet( child: Container( height: 200, child: Text('Hello, World!'), ), // ... ) : Container( height: 50, child: Text('Hello, World!'), ), ) ``` 以上代码实现了一个点击展开或收起的交互效果,根据 `_expanded` 变量的值来判断当前应该展开还是收起。当 `_expanded` 为 `true` 时,展示动画组件;否则展示一个高度为 50 的简单组件。 通过以上步骤,就可以实现一个简单的展开收起动画了。需要注意的是,动画效果的具体实现可以根据需求进行调整,例如可以使用 `Tween` 设置不同的动画属性,使用不同的曲线来调整动画速度等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值