Flutter 动画容器更改子部件的大小

发布于 2025-01-11 12:02:41 字数 3319 浏览 0 评论 0原文

我正在尝试创建一个容器,在其中可以执行两个手势:onTapDownonTapUp。我想在执行 onTapDown 时减小容器的尺寸。

我想要的是这样的: https://i.imgur.com/7hW2Cn1.gifv

问题是,如果我工作使用 AnimatedController 我还需要调整孩子们的大小,这很混乱。

我还查看了 flutter_bounce 库,但我想要的不是基于持续时间的东西。如果用户保持按下状态,则容器保持按下状态。

class CustomContainerState extends State<CustomContainer> {

  double width = 90.w;
  double height = 35.h;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Center( 
        child: AnimatedContainer(
          duration: Duration(milliseconds: 200),
          margin: EdgeInsets.only(left: 5.w, right: 5.w),
          width: width,
          height: height,
          child: Column(
              children: [
                Container(
                  width: 90.w,
                  height: 25.h,
                  decoration: BoxDecoration(
                    color: green400,
                    borderRadius: BorderRadius.only(topLeft: Radius.circular(5.w), topRight: Radius.circular(5.w)),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 5.w, right: 5.w, top: 1.h, bottom: 1.h),
                  width: 90.w,
                  height: 8.h,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(bottomLeft: Radius.circular(5.w), bottomRight: Radius.circular(5.w)),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Text(
                        "Ratings",
                        style: TextStyle(
                          fontFamily: "SanFrancisco",
                          fontSize: 10.sp,
                          fontWeight: FontWeight.w400,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      Text(
                        "Title",
                        style: TextStyle(
                          fontFamily: "SanFrancisco",
                          fontSize: 12.sp,
                          fontWeight: FontWeight.w600,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      Text(
                        "Description",
                        style: TextStyle(
                          fontFamily: "SanFrancisco",
                          fontSize: 10.sp,
                          fontWeight: FontWeight.w400,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
          ),
        ),
        
           
      ),

      onTapDown: (tapDownDetails) {
        setState(() {
          width = 85.w;
          height = 30.h;
        });
      },
      onTapUp: (TapUpDetails) {
        setState(() {
          width = 90.w;
          height = 35.h;
        });
      },
    );
  }
}

问题是如何仅减少父容器并自动减少子容器?因为我也可以为孩子使用多个 AnimatedContainer,但问题是文本没有动画。

I'm trying to create a container in which I can perform two gesture: onTapDown and onTapUp. I want to decrease the dimension of my container when I perform onTapDown.

What I want is something like that:
https://i.imgur.com/7hW2Cn1.gifv

The problem is that if I work with AnimatedController I need to resize also the children and it's a mess.

I also looked to the flutter_bounce library but what I want is not something based on a duration. If the user keep pressed the container maintains the state of pressed.

class CustomContainerState extends State<CustomContainer> {

  double width = 90.w;
  double height = 35.h;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Center( 
        child: AnimatedContainer(
          duration: Duration(milliseconds: 200),
          margin: EdgeInsets.only(left: 5.w, right: 5.w),
          width: width,
          height: height,
          child: Column(
              children: [
                Container(
                  width: 90.w,
                  height: 25.h,
                  decoration: BoxDecoration(
                    color: green400,
                    borderRadius: BorderRadius.only(topLeft: Radius.circular(5.w), topRight: Radius.circular(5.w)),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 5.w, right: 5.w, top: 1.h, bottom: 1.h),
                  width: 90.w,
                  height: 8.h,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(bottomLeft: Radius.circular(5.w), bottomRight: Radius.circular(5.w)),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Text(
                        "Ratings",
                        style: TextStyle(
                          fontFamily: "SanFrancisco",
                          fontSize: 10.sp,
                          fontWeight: FontWeight.w400,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      Text(
                        "Title",
                        style: TextStyle(
                          fontFamily: "SanFrancisco",
                          fontSize: 12.sp,
                          fontWeight: FontWeight.w600,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      Text(
                        "Description",
                        style: TextStyle(
                          fontFamily: "SanFrancisco",
                          fontSize: 10.sp,
                          fontWeight: FontWeight.w400,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
          ),
        ),
        
           
      ),

      onTapDown: (tapDownDetails) {
        setState(() {
          width = 85.w;
          height = 30.h;
        });
      },
      onTapUp: (TapUpDetails) {
        setState(() {
          width = 90.w;
          height = 35.h;
        });
      },
    );
  }
}

The problem so is how to decrease only the parent container that automatically decrease also the children? Because I can use multiple AnimatedContainer also for the children but the problem is that the text is not animated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2025-01-18 12:02:41

您是否尝试过使用 scale 而不是更改宽度和高度?像这样的东西:

class CustomContainerState extends State<CustomContainer> {
  double width = 90;
  double height = 350;
  double scale = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: GestureDetector(
          child: Center(
            child: AnimatedContainer(
              transform: Matrix4.identity()..scale(scale),
              color: Colors.green,
              duration: Duration(milliseconds: 200),
              margin: EdgeInsets.only(left: 5, right: 5),
              width: width,
              height: height,
              child: Column(
                children: [
                  Container(
                    width: 90,
                    height: 25,
                    decoration: BoxDecoration(
                      // color: green400,
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(5),
                          topRight: Radius.circular(5)),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(
                        left: 5, right: 5, top: 1, bottom: 1),
                    width: 90,
                    height: 80,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(5),
                          bottomRight: Radius.circular(5)),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Text(
                          "Ratings",
                          style: TextStyle(
                            fontFamily: "SanFrancisco",
                            fontSize: 10,
                            fontWeight: FontWeight.w400,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        Text(
                          "Title",
                          style: TextStyle(
                            fontFamily: "SanFrancisco",
                            fontSize: 12,
                            fontWeight: FontWeight.w600,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        Text(
                          "Description",
                          style: TextStyle(
                            fontFamily: "SanFrancisco",
                            fontSize: 10,
                            fontWeight: FontWeight.w400,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          onTapDown: (tapDownDetails) {
            setState(() {
              // width = 85;
              // height = 300;
              scale = .8;
            });
          },
          onTapUp: (TapUpDetails) {
            setState(() {
              // width = 90;
              // height = 350;
              scale = 1;
            });
          },
        ),
      ),
    );
  }
}

Have you tried with scale instead of changing width and height? Something like this:

class CustomContainerState extends State<CustomContainer> {
  double width = 90;
  double height = 350;
  double scale = 1;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: GestureDetector(
          child: Center(
            child: AnimatedContainer(
              transform: Matrix4.identity()..scale(scale),
              color: Colors.green,
              duration: Duration(milliseconds: 200),
              margin: EdgeInsets.only(left: 5, right: 5),
              width: width,
              height: height,
              child: Column(
                children: [
                  Container(
                    width: 90,
                    height: 25,
                    decoration: BoxDecoration(
                      // color: green400,
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(5),
                          topRight: Radius.circular(5)),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(
                        left: 5, right: 5, top: 1, bottom: 1),
                    width: 90,
                    height: 80,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(5),
                          bottomRight: Radius.circular(5)),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Text(
                          "Ratings",
                          style: TextStyle(
                            fontFamily: "SanFrancisco",
                            fontSize: 10,
                            fontWeight: FontWeight.w400,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        Text(
                          "Title",
                          style: TextStyle(
                            fontFamily: "SanFrancisco",
                            fontSize: 12,
                            fontWeight: FontWeight.w600,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                        Text(
                          "Description",
                          style: TextStyle(
                            fontFamily: "SanFrancisco",
                            fontSize: 10,
                            fontWeight: FontWeight.w400,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
          onTapDown: (tapDownDetails) {
            setState(() {
              // width = 85;
              // height = 300;
              scale = .8;
            });
          },
          onTapUp: (TapUpDetails) {
            setState(() {
              // width = 90;
              // height = 350;
              scale = 1;
            });
          },
        ),
      ),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文