如何在背景打开/关闭颤动期间更改AppBar文本

时间:2018-12-09 17:31:06

标签: flutter flutter-layout appbar flutter-animation

我在应用中实现了背景幕。我想在背景打开/关闭期间更改AppBar文本。我尝试使用Boolean isOpen在IconButton onPressed运行期间使其为true / false,但是我想知道哪种方法是理想的。是否有任何API可检测背景幕的打开/关闭?

    import 'package:expense_manager_app/widget/navigation_panel.dart';
import 'package:flutter/material.dart';

class BackDropPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _BackDropState();
  }
}

class _BackDropState extends State<BackDropPage>
    with SingleTickerProviderStateMixin {
  AnimationController controller;
  var mTitle = "Home";
  var mIsOpen = false;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(
        vsync: this, duration: Duration(milliseconds: 100), value: 1.0);
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  bool get isPanelVisible {
    final AnimationStatus status = controller.status;
    return status == AnimationStatus.completed ||
        status == AnimationStatus.forward;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('$mTitle'),
        elevation: 0.0,
        leading: new IconButton(
          onPressed: () {
            controller.fling(velocity: isPanelVisible ? -1.0 : 1.0);
            setState(() {
              if (mIsOpen) {
                mIsOpen = false;
                mTitle = "Home";
              } else {
                mTitle = "Menu";
                mIsOpen = true;
              }
            });
          },
          icon: new AnimatedIcon(
            icon: AnimatedIcons.close_menu,
            progress: controller.view,
          ),
        ),
      ),
      body: NavigationPanelPage(controller: controller),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

最后,我使用isPanelVisible bool完成了自己的工作。

appBar: AppBar(
    title: Text('$mTitle'),
    elevation: 0.0,
    leading: new IconButton(
      onPressed: () {
        controller.fling(velocity: isPanelVisible ? -1.0 : 1.0);
        setState(() {
          if (isPanelVisible) {
            mTitle = "Home";
          } else {
            mTitle = "Menu";
          }
        });
      },
      icon: new AnimatedIcon(
        icon: AnimatedIcons.close_menu,
        progress: controller.view,
      ),
    ),
  ),