颤动中的自定义底栏

时间:2020-01-08 06:41:10

标签: flutter dart flutter-layout flutter-animation

我是新手。我想让底部的条看起来像第一张图片

图片1 enter image description here

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

图片2 enter image description here

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

图标仅供参考。

我能够像image1那样使底部条形,但不知道如何扩展它。

请帮助

1 个答案:

答案 0 :(得分:2)

您可以尝试此解决方案

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);
  }
}