简体   繁体   English

如何使用Function在Button内部调用

[英]How to Use Function to Call it inside the Button

Currently, I use Navigation to bring up Dialog.目前,我使用导航来调出对话框。 But of course, the page will be changed, and the screen will only display the Dialog.但是当然,页面会发生变化,屏幕上只会显示Dialog。 Like I've shown below:就像我在下面展示的那样:

在此处输入图像描述

I want to fix it, so when the Dialog appear, the page behind it wouldn't disappear, like the picture below.我想修复它,所以当Dialog出现时,它后面的页面不会消失,如下图。

在此处输入图像描述

This is my code, with NavigationHelper, the screen will move to another screen to display Dialog, which I want to change that.这是我的代码,使用 NavigationHelper,屏幕将移动到另一个屏幕以显示对话框,我想更改它。

PopupMenuItem(
     onTap: () {
          GetIt.I<NavigationHelper>().go('/alert_delete_item');
     },
     value: 'Delete',

And this is a template to handle the Dialog, but I do not know how to use it.这是一个处理对话框的模板,但我不知道如何使用它。

Future<T?> showCustomDialog<T>(BuildContext context) {
    return showDialog<T>(
      context: context,
      builder: (context) => Dialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(16),
        ),
        child: this,
      ),
    );
  }

Any tutorial how to use function?任何教程如何使用 function?

Should try this应该试试这个

  import 'package:flutter/material.dart';

  void main() {runApp(new MyApp());}

  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Flutter',
        home: Scaffold(
          appBar: AppBar(title: Text('Dummy Screen')), 
          body: HomePage()
        )
      );
    }
  }

  class HomePage extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return GestureDetector(
        onTap: () {showAlertDialoge(context);},
        child: ListView.builder(
          itemCount: 20,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text("Heading ${index}" , style: Theme.of(context).textTheme.headline5,),
              subtitle: Text("This is subtitle ${index}" ,),
            );
          },
        ),
      );
    }

    void showAlertDialoge(BuildContext context) {
      showDialog(
        context: context,
        builder: (context) => AlertDialog(
          content: Text("Dialog Box Occur in front of screen", style: TextStyle(fontSize: 16),),
        )
      );
    }
  }

OutPut: OutPut:

在此处输入图像描述

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

相关问题 如何在 flutter PopupMenuButton 中调用异步 function? - How to call an asynchronous function inside flutter PopupMenuButton? 如何在 function 外部的有状态小部件内部调用 flutter function? - how to call flutter function that is inside stateful widget in outside function? 如何在 flutter 中的另一个异步 function 中调用一个异步 function - How to call one async function inside another async function in flutter 如何调用和等待异步 function 在 flutter 中同步 function 完成 - How to call and wait async function done inside sync function in flutter 如何使用 onGenerateRoute 回调 function - how to use call back function with onGenerateRoute 我如何在 function 中使用包含? - how can i use contains inside function? 如何在 statelessWidget 类中使用 setState() 函数 - How to use setState() function inside statelessWidget class Flutter-如何在继承的小部件中的已定义函数中使用参数 - Flutter - How to use parameters inside defined function inside inherited widget Flutter,如何从返回的 Widget 调用 Stateful Widget 内部的函数? - Flutter, how to call a function inside Stateful Widget from a returned Widget? 如何从另一个小部件中某处的按钮调用 StatefulWidget 中的函数? - How to call function in a StatefulWidget from a button somewhere within another widget?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM