【问题标题】:How to make popup menu from bottomnavigationbar in Flutter?如何从 Flutter 的底部导航栏制作弹出菜单?
【发布时间】:2021-03-03 16:16:18
【问题描述】:

IM 试图在此屏幕截图中实现类似的效果,即底部导航栏在页面之间切换,但中间按钮启动底部工作表或其他操作(如弹出菜单)并停留在该页面上......

class _HomePageState extends State<HomePage> {
  PersistentTabController _controller;

  @override
  void initState() {
    super.initState();

    _controller = PersistentTabController(initialIndex: 0);
    _hideNavBar = false;
  }

  List<Widget> _buildScreens() {
    return [
      MyHomePage(),

      AddPage(), // this need to be action button not new page...

      MyActivity(),
    ];
  }
  List<PersistentBottomNavBarItem> _navBarsItems() {
    return [
      PersistentBottomNavBarItem(
        icon: Icon(Icons.home),
        title: "Home",
        activeColor: Colors.purpleAccent,
        inactiveColor: Colors.grey,
      ),
      PersistentBottomNavBarItem(
        icon: Icon(Icons.add),
        title: ("Add"),
        activeColor: Colors.purpleAccent,
        inactiveColor: Colors.grey,
      ),
      PersistentBottomNavBarItem(
        icon: Icon(Icons.search),
        title: ("MyAct"),
        activeColor: Colors.purpleAccent,
        inactiveColor: Colors.grey,
      ),
    ];
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PersistentTabView.custom(
        context,
        controller: _controller,
        screens: _buildScreens(),
        confineInSafeArea: true,
        itemCount: 3,
        handleAndroidBackButtonPress: true,
        resizeToAvoidBottomInset: false,
        stateManagement: true,
        hideNavigationBar: _hideNavBar,
        screenTransitionAnimation: ScreenTransitionAnimation(
          animateTabTransition: true,
          curve: Curves.ease,
          duration: Duration(milliseconds: 200),
        ),
        customWidget: CustomNavBarWidget(
          items: _navBarsItems(),
          onItemSelected: (index) {

            setState(() {
              _controller.index = index; // THIS IS CRITICAL!! Don't miss it!
            });
          },
          selectedIndex: _controller.index,
        ),
      ),
);
}

IM使用persistenNavBar,但我认为与常规底部导航情况是一样的... 我想我可以制作自定义的底部导航栏,或者使用不同的小部件类型的列表?

【问题讨论】:

    标签: flutter bottomnavigationview popupmenu bottombar


    【解决方案1】:

    你可以使用

    showModalBottomSheet()
    

    来自 Flutter 并设置参数如下:

    showModalBottomSheet(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), //for the round edges
        builder: (context) {
            return Container(
                //specify height, so that it does not fill the entire screen
                child: Column(children: []) //what you want to have inside, I suggest using a column
            );
        }
        context: context,
        isDismissible: //boolean if you want to be able to dismiss it
        isScrollControlled: //boolean if something does not display, try that
    );
    

    然后你就可以在BottomNavigationBar点击执行这个功能了。

    【讨论】:

    • 是的,但是 IM 使用 _curentindex(_controler.index) 和所有底部表单弹出,页面仍然切换,我想以某种方式留在同一页面上......
    • 那么不要将当前索引改回之前的索引,这样页面就不会改变?
    • 是的,我试图找出最好的方法,co 的索引在“onItemSelected”上发生了变化,所以我需要一些条件逻辑......反正 tnx
    • 如果我的回答回答了你的问题,那么标记为答案:) np,祝你好运
    猜你喜欢
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 2020-05-06
    • 2022-01-18
    • 2019-01-31
    • 1970-01-01
    相关资源
    最近更新 更多