简体   繁体   English

当用户点击 BottomNavigationBarItem 时如何显示 flutter modalBottomSheet

[英]How to display a flutter modalBottomSheet when user taps a BottomNavigationBarItem

I would like to display a modalBottomsheet when user taps a BottomNavigationBarItem, i tried to put the code in a function and call it in the body of a scaffold but that did not work.. any ideas on how to do this?当用户点击 BottomNavigationBarItem 时,我想显示一个 modalBottomsheet,我试图将代码放在 function 中并在脚手架的主体中调用它,但这不起作用..关于如何做到这一点的任何想法? see my code below在下面查看我的代码

import 'package:flutter/material.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';

class More extends StatefulWidget {
  More({Key key}) : super(key: key);

  @override
  _MoreState createState() => _MoreState();
}

class _MoreState extends State<More> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            'Modal Bottom Sheet',
            style: TextStyle(color: Colors.white),
          ),
          centerTitle: true,
        ),
        body: displayModal());
  }

  displayModal() {
    showBottomSheet(
      context: context,
      builder: (context) => Container(child: Text("data")),
    );
  }
}

showBottomSheet is a async function. showBottomSheet是异步 function。 you should use something like this你应该使用这样的东西

 body: Center(
          child: ElevatedButton(
            onPressed: () {
              displayModal();
            },
            child: Text("show dialog"),
          ),
        )

if you want to show the dialog on widget load, call the dialog in initState of the statefull widget如果要在小部件加载时显示对话框,请在有状态小部件的 initState 中调用对话框

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      displayModal();
    });
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Flutter 中为 BottomNavigationBarItem 添加徽章? - How to add badge to BottomNavigationBarItem in Flutter? BottomNavigationBarItem label 在 flutter 中选择时不显示 - BottomNavigationBarItem label not showing when selected in flutter 如何在用户点击按钮时终止应用程序 - How to kill an application when the user taps a button 如何检测用户何时点按Android中的视图 - How to detect when a user taps on a view in Android 当用户点击推送通知消息时,我的应用程序启动。 如何使应用程序完整显示推送消息? - When user taps on the push notification message, my app launches. How to make the app display the push message in full? 如何在固定时间段内在android中显示“游戏结束”图像并在指定间隔后用户点击屏幕时重新启动游戏? - how to display the “Game over” image in android for a fixed amount of time and restart the game when user taps the screen after a specified interval? 用户点击屏幕时如何退出无限while循环? - How to exit an infinite while loop when user taps screen? 如何使 Flutter BottomNavigationBarItem 图标和 label 在同一行 - How to make Flutter BottomNavigationBarItem icon and label in same line 如何在flutter中将徽章计数数字添加为BottomNavigationBarItem的一部分 - How to add a badge count number as part of a BottomNavigationBarItem in flutter 当用户点击MapView中的OverlayItem时,显示简单的文本标签/描述气泡 - Display a simple text label / descrption bubble when user taps on a OverlayItem in MapView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM