【问题标题】:Custom bottombar in flutter颤振中的自定义底栏
【发布时间】:2020-01-08 06:41:10
【问题描述】:

我是新来的颤振。我想让底栏看起来像第一张图片

Image1

当点击中间的向上箭头时,它应该会展开并显示更多图标(看起来像第二张图片)。

图片2

再次单击箭头时,它将隐藏展开的部分

图标仅供参考。

我可以把底栏做成 image1,但不知道如何扩展它。

请帮忙

【问题讨论】:

    标签: flutter dart flutter-layout flutter-animation


    【解决方案1】:

    你可以试试这个解决方案

    import 'package:flutter/material.dart';
    
    void main() => runApp(MaterialApp(
          home: MyApp(),
        ));
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      bool clicked = false;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          bottomNavigationBar: Container(
            child: Wrap(
              children: <Widget>[
                HiddenBottomNavigationBar(
                  open: clicked,
                  child: Container(
                    color: Colors.grey,
                    width: MediaQuery.of(context).size.width,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        IconButton(
                          onPressed: () {},
                          icon: Icon(Icons.favorite),
                        ),
                        IconButton(
                          onPressed: () {},
                          icon: Icon(Icons.supervised_user_circle),
                        ),
                        IconButton(
                          onPressed: () {},
                          icon: Icon(Icons.settings),
                        ),
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  height: 10,
                ),
                buildRow()
              ],
            ),
          ),
        );
      }
    
      Row buildRow() {
        return Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: <Widget>[
            IconButton(
              onPressed: () {},
              icon: Icon(Icons.account_balance_wallet),
            ),
            IconButton(
              onPressed: () {
                setState(() {
                  clicked = !clicked;
                });
              },
              icon: Icon(clicked ? Icons.expand_more : Icons.expand_less),
            ),
            IconButton(
              onPressed: () {},
              icon: Icon(Icons.save),
            ),
          ],
        );
      }
    }
    
    class HiddenBottomNavigationBar extends StatefulWidget {
      final Widget child;
      final bool open;
    
      HiddenBottomNavigationBar({this.open = false, this.child});
    
      @override
      _HiddenBottomNavigationBarState createState() =>
          _HiddenBottomNavigationBarState();
    }
    
    class _HiddenBottomNavigationBarState extends State<HiddenBottomNavigationBar>
        with SingleTickerProviderStateMixin {
      AnimationController openController;
      Animation<double> animation;
    
      @override
      void initState() {
        super.initState();
        initAnimations();
      }
    
      initAnimations() {
        openController =
            AnimationController(vsync: this, duration: Duration(milliseconds: 500));
        Animation curve = CurvedAnimation(
          parent: openController,
          curve: Curves.easeInBack
        );
        animation = Tween(begin: 0.0, end: 1.0).animate(curve)
          ..addListener(() {
            setState(() {});
          });
      }
    
      @override
      void didUpdateWidget(HiddenBottomNavigationBar oldWidget) {
        super.didUpdateWidget(oldWidget);
        if (widget.open) {
          openController.forward();
        } else {
          openController.reverse();
        }
      }
    
      @override
      void dispose() {
        openController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return SizeTransition(
            axisAlignment: 1.0, sizeFactor: animation, child: widget.child);
      }
    }
    

    【讨论】:

    • 感谢您的帮助。它对我有很大帮助。我们可以在单击箭头时为隐藏和显示设置动画吗?
    • 你想实现什么样的动画?一点描述会很有帮助
    • 像一个抽屉。上下开合
    • 我已经用动画编辑了答案。我希望它有所帮助。如果对您有帮助,请接受答案。
    • 谢谢。它运作良好。但有一个问题 - 当底部栏展开时,底部溢出 45 个像素。我在正文中使用综合浏览量。
    猜你喜欢
    • 2020-08-09
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2020-12-05
    • 1970-01-01
    • 2022-07-27
    相关资源
    最近更新 更多