【问题标题】:Flutter Switching FAB on PageView Pages with animation in ScaffoldFlutter Switching FAB on PageView Pages with Animation in Scaffold
【发布时间】:2019-05-19 05:18:29
【问题描述】:

因此,我已经查看了几个地方以了解应该如何实现它,并且我确定我错过了一些微不足道的东西,但是我有一个颤动的应用程序,它的主体是一个 PageView。我需要为某些页面设置不同的 FAB,它当前的设置方式是脚手架的 floatingActionButton 属性设置为访问索引为 _currentPageIndex 的 FloatingActionButtons 数组(bottomNavBar 和 _pageController 共享的私有变量。

这会突然改变 FAB,这不是预期的行为。

我正在尝试让 FAB 在页面发生变化时(如material spec)进行动画处理:

选项卡式屏幕 当存在标签时,FAB应该短暂消失,然后>新内容进入到位时重新出现。这表示 >FAB 未连接到任何特定选项卡。

对于如何简单地实现它的任何建议,我将不胜感激(我很确定我错过了一些微不足道的东西)。另一种方法是通过将 FAB 包裹在某些东西中来手动为进出 FAB 设置动画。

【问题讨论】:

    标签: android user-interface animation flutter floating-action-button


    【解决方案1】:

    您可以尝试使用类似这样的AnimatedCrossFade Widget:

            class TestingNewWidgetState extends State<TestingNewWidget> {
              int currentIndex = 0;
    
              @override
              Widget build(BuildContext context) {
                return Scaffold(
                  floatingActionButton: AnimatedCrossFade(
                    crossFadeState: currentIndex == 0
                        ? CrossFadeState.showFirst
                        : CrossFadeState.showSecond,
                    duration: Duration(seconds: 1),
                    firstChild: FloatingActionButton(
                      onPressed: () => null,
                      child: Icon(Icons.arrow_left),
                      backgroundColor: Colors.red,
                    ),
                    secondChild: FloatingActionButton(
                      onPressed: () => null,
                      child: Icon(Icons.arrow_right),
                      backgroundColor: Colors.blue,
                    ),
                  ),
                  body: PageView(
                    onPageChanged: (index) {
                      setState(() {
                        currentIndex = index;
                      });
                    },
                    children: <Widget>[
                      Scaffold(
                        body: Center(
                          child: Text("page 1"),
                        ),
                      ),
                      Scaffold(
                        body: Center(
                          child: Text("page 2"),
                        ),
                      ),
                    ],
                  ),
                );
              }
            }
    

    更新

    请记住,您可以创建自己的小部件,这是使用自定义 FloatingActionButton 的示例:

            class TestingNewWidgetState extends State<TestingNewWidget> {
              int currentIndex = 0;
    
              @override
              Widget build(BuildContext context) {
                var customFabButton;
                if (currentIndex == 0) {
                  customFabButton = CustomFabButton(
                    color: Colors.red,
                    onPressed: () => null,
                    icon: Icons.alarm,
                  );
                } else if (currentIndex == 1) {
                  customFabButton = CustomFabButton(
                    color: Colors.blue,
                    onPressed: () => null,
                    icon: Icons.satellite,
                  );
                } else {
                  customFabButton = CustomFabButton(
                    color: Colors.green,
                    onPressed: () => null,
                    icon: Icons.verified_user,
                  );
                }
    
                return Scaffold(
                  floatingActionButton: customFabButton,
                  body: PageView(
                    onPageChanged: (index) {
                      setState(() {
                        currentIndex = index;
                      });
                    },
                    children: <Widget>[
                      Scaffold(
                        body: Center(
                          child: Text("page 1"),
                        ),
                      ),
                      Scaffold(
                        body: Center(
                          child: Text("page 2"),
                        ),
                      ),
                      Scaffold(
                        body: Center(
                          child: Text("page 3"),
                        ),
                      ),
                    ],
                  ),
                );
              }
            }
    
            class CustomFabButton extends StatelessWidget {
              final IconData icon;
              final Color color;
              final VoidCallback onPressed;
    
              const CustomFabButton({Key key, this.icon, this.color, this.onPressed})
                  : super(key: key);
    
              @override
              Widget build(BuildContext context) {
                return GestureDetector(
                  onTap: onPressed,
                  child: AnimatedContainer(
                    decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: color,
                    ),
                    duration: Duration(seconds: 1),
                    height: 50.0,
                    width: 50.0,
                    child: Icon(icon),
                  ),
                );
              }
            }
    

    【讨论】:

    • 这将在两个 FAB 之间,但我有 5 页,其中 3 页有 FAB。我可以根据状态切换第一个和第二个孩子来解决这个问题,但我觉得还应该有一个更简单的解决方案,默认情况下可以工作。我可能需要设置外部脚手架上的某些属性?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 2019-04-09
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多