将 BoxShadow 应用于 SizeTransition 的容器子部分 Flutter。

huangapple go评论66阅读模式
英文:

BoxShadow to Container child of SizeTransition Flutter

问题

我创建了一个拉伸的 nabar,在我点击 "autres" 图标时,有一个像下面屏幕一样的过渡。

我正在尝试为一个作为 SizeTransition 子级的 Container 添加 BoxShadow。

我写了这段代码:

这是沙盒链接: https://flutlab.io/editor/1c7de2dd-edc4-4e09-b1b3-4a4d63f1a162

@override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
///这里是我谈论的部分
        SizeTransition(
          sizeFactor: _animation,
          axisAlignment: -1,
          child: Container(
            decoration: BoxDecoration(
              color: ColorResources.white,
              boxShadow: [
                BoxShadow(
                  color: const Color.fromARGB(255, 0, 0, 0).withOpacity(0.5),
                  spreadRadius: 2,
                  blurRadius: 7,
                  offset: Offset(0, 3),
                ),
              ],
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                GestureDetector(
                  onTap: () {},
                  child: const SizedBox(
                    height: kBottomNavigationBarHeight,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.menu_outlined),
                        const SizedBox(height: 0.0),
                        Text(
                          "Menu",
                          style: TextStyle(
                            fontSize: 12.0,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Container(
          decoration: BoxDecoration(
            boxShadow: [
              BoxShadow(
                color: const Color.fromARGB(255, 0, 0, 0).withOpacity(0.5),
                spreadRadius: 5,
                blurRadius: 7,
                offset: const Offset(0, 3), // changes position of shadow
              ),
            ],
          ),
          child: BottomNavigationBar(
            backgroundColor: ColorResources.white,
            type: BottomNavigationBarType.fixed,
            currentIndex: _selectedIndex,
            fixedColor: ColorResources.blue,
            onTap: _onItemTapped,
            items: [
              BottomNavigationBarItem(
                icon: Icon(Icons.menu_outlined),
                label: "Fil d'actu",
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
                label: 'Idées',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.output),
                label: 'Entraide',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.keyboard_option_key_outlined),
                label: 'Projets',
              ),
              const BottomNavigationBarItem(
                icon: Icon(Icons.menu_outlined),
                label: 'Autres',
              ),
            ],
          ),
        ),
      ],
    );
  }

目前,我得到的唯一结果是这样的:

将 BoxShadow 应用于 SizeTransition 的容器子部分 Flutter。

我如何添加与导航栏相同的阴影?

英文:

I created a stretch nabar, when I click on the "autres" icon, there's a transition like the screen below.

I'm trying to put a BoxShadow to a Container which is child of a SizeTransition.

I made this code:

Here's the sandbox: https://flutlab.io/editor/1c7de2dd-edc4-4e09-b1b3-4a4d63f1a162

@override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
///Here's the part i talk about
        SizeTransition(
          sizeFactor: _animation,
          axisAlignment: -1,
          child: Container(
            decoration: BoxDecoration(
              color: ColorResources.white,
              boxShadow: [
                BoxShadow(
                  color: const Color.fromARGB(255, 0, 0, 0).withOpacity(0.5),
                  spreadRadius: 2,
                  blurRadius: 7,
                  offset: Offset(0, 3),
                ),
              ],
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                GestureDetector(
                  onTap: () {},
                  child: const SizedBox(
                    height: kBottomNavigationBarHeight,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(Icons.menu_outlined),
                        const SizedBox(height: 0.0),
                        Text(
                          "Menu",
                          style: TextStyle(
                            fontSize: 12.0,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
        Container(
          decoration: BoxDecoration(
            boxShadow: [
              BoxShadow(
                color: const Color.fromARGB(255, 0, 0, 0).withOpacity(0.5),
                spreadRadius: 5,
                blurRadius: 7,
                offset: const Offset(0, 3), // changes position of shadow
              ),
            ],
          ),
          child: BottomNavigationBar(
            backgroundColor: ColorResources.white,
            type: BottomNavigationBarType.fixed,
            currentIndex: _selectedIndex,
            fixedColor: ColorResources.blue,
            onTap: _onItemTapped,
            items: [
              BottomNavigationBarItem(
                icon: Icon(Icons.menu_outlined),
                label: "Fil d'actu",
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.ac_unit),
                label: 'Idées',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.output),
                label: 'Entraide',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.keyboard_option_key_outlined),
                label: 'Projets',
              ),
              const BottomNavigationBarItem(
                icon: Icon(Icons.menu_outlined),
                label: 'Autres',
              ),
            ],
          ),
        ),
      ],
    );
  }

For the moment, the only result that I have is like this:

将 BoxShadow 应用于 SizeTransition 的容器子部分 Flutter。

How can I add the same Shadow as the navbar?

答案1

得分: 1

以下是翻译好的内容:

原因是SizeTransition小部件上的阴影不可见的原因是在动画之前它没有尺寸,所以阴影不可见。一个解决方案是用一个Container包裹SizeTransition小部件,这样阴影就能显示出来。

我们还将SizedBox的高度设置为kBottomNavigationBarHeight(你也可以设置自己想要的高度)。

这是更新后的代码片段:

Container(
  decoration: BoxDecoration(
    boxShadow: [
      BoxShadow(
        color: const Color.fromARGB(255, 0, 0, 0).withOpacity(0.5),
        spreadRadius: 5,
        blurRadius: 7,
        offset: const Offset(0, 3), // 改变阴影位置
      ),
    ],
  ),
  child: SizeTransition(
    sizeFactor: _animation,
    axisAlignment: -1,
    child: Container(
      decoration: BoxDecoration(
        color: Colors.white
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          GestureDetector(
            onTap: () {},
            child: SizedBox(
              height: kBottomNavigationBarHeight,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.menu_outlined),
                  const SizedBox(height: 0.0),
                  Text(
                    "菜单",
                    style: TextStyle(
                      fontSize: 12.0,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    ),
  ))

这样就会为SizeTransition小部件创建一个固定高度的容器,使阴影可见。

英文:

The reason the shadow is not appearing on the SizeTransition widget is that it does not have a size until it is animated, so the shadow is not visible. One solution is to wrap the SizeTransition widget with a Container, which will allow the shadow to be visible.

We also add height to the SizedBox to kBottomNavigationBarHeight (you can set your own height, instead)

Here's the updated code snippet:

Container(
  decoration: BoxDecoration(
    boxShadow: [
      BoxShadow(
        color: const Color.fromARGB(255, 0, 0, 0).withOpacity(0.5),
        spreadRadius: 5,
        blurRadius: 7,
        offset: const Offset(0, 3), // changes position of shadow
      ),
    ],
  ),
  child: SizeTransition(
    sizeFactor: _animation,
    axisAlignment: -1,
    child: Container(
      decoration: BoxDecoration(
        color: Colors.white
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          GestureDetector(
            onTap: () {},
            child: SizedBox(
              height: kBottomNavigationBarHeight,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Icon(Icons.menu_outlined),
                  const SizedBox(height: 0.0),
                  Text(
                    "Menu",
                    style: TextStyle(
                      fontSize: 12.0,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    ),
  ))

This should create a fixed height container for the SizeTransition widget, allowing the shadow to be visible.

Result:
将 BoxShadow 应用于 SizeTransition 的容器子部分 Flutter。

huangapple
  • 本文由 发表于 2023年4月17日 15:48:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定