如何通过在Flutter中单击“屏幕”的其他区域来管理警报对话框?

问题描述

我的应用程序中有活动。我正在使用“警报对话框”。当我进入此活动并且“警报对话框”打开时,当我单击屏幕的其他区域时,它消失了,但是问题是,当我想单击任何文本字段的内部时,它会一次又一次地出现。我希望这样,一旦消失,新的再次出现,直到我转到其他活动并再次返回此活动。我是Flutter的新手,几天来我就遇到了这个问题,我已经多次搜索了此问题,但没有找到解决该问题的方法,任何可以帮助我的人,在此先感谢。 代码

    import 'package:Flutter/cupertino.dart';
import 'package:Flutter/material.dart';
import 'bottomnavigationbar.dart';

class Contact extends StatefulWidget {
  @override
  _ContactState createState() => _ContactState();
}

class _ContactState extends State<Contact> {
  @override
  Widget build(BuildContext context) {
    AlertDialog dialog = new AlertDialog(
      content: Container(
        width: 260.0,height: 187.0,decoration: new Boxdecoration(
          shape: BoxShape.rectangle,//  color: const Color(0xFFFFFF),borderRadius: new BorderRadius.all(new Radius.circular(50.0)),),child: Column(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.all(1),Container(
              child: GestureDetector(
                onTap: () {
//                Route route =
//                    MaterialPageRoute(builder: (context) => Payment1());
//                Navigator.pushReplacement(context,route);
                  //  Navigator.pushNamed(context,"myRoute");
                },child: new Text(
                  "For Bikes-979578-6541",textAlign: TextAlign.center,style: TextStyle(
                    color: Colors.red,fontSize: 18,//  fontWeight: FontWeight.w700,Padding(
              padding: EdgeInsets.all(1),Divider(
              thickness: 1.5,child: new Text(
                  "For Boats-979-541-3607",//fontWeight: FontWeight.w700,child: new Text(
                  "redtiderentals@yahoo.com",// fontWeight: FontWeight.w700,child: new Text(
                  "Follow Us on Facebook!",Container(
              child: GestureDetector(
                onTap: () {
                  //    Navigator.pop(dialog);
//                Route route =
//                    MaterialPageRoute(builder: (context) => Payment1());
//                Navigator.pushReplacement(context,"myRoute");
                  //   Navigator.of(context).pop(false),},child: new Text(
                  "Done",],);
    Future.delayed(
      Duration.zero,() => showDialog(
          context: context,builder: (BuildContext context) => dialog),);

    TextStyle style = TextStyle(
        fontFamily: 'Montserrat',color: Colors.white,fontSize: 16.0);

    return Scaffold(
      appBar: AppBar(
        title: Text("Contact"),centerTitle: true,actions: <Widget>[
          new IconButton(
            icon: new Icon(
              Icons.more_horiz,size: 35,//  tooltip: 'Closes application',//   onpressed: () => exit(0),body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.all(1),color: Colors.black,child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[
              Container(
                width: MediaQuery.of(context).size.width,height: MediaQuery.of(context).size.width / 1.6,//  constraints: BoxConstraints.expand(),decoration: Boxdecoration(
                  image: decorationImage(
                      image: Assetimage("images/contactpage.png"),fit: BoxFit.cover),Padding(
                padding: EdgeInsets.all(8),Text(
                "Contact Info",style: TextStyle(
                  color: Colors.red,fontSize: 20,fontWeight: FontWeight.w400,Divider(
                color: Colors.white,indent: 40,endindent: 40,Container(
                  padding: EdgeInsets.all(16),child: Column(
                    //mainAxisAlignment: MainAxisAlignment.center,crossAxisAlignment: CrossAxisAlignment.start,children: [
                      Text(
                        "We are a locally owned. family run business providing equipment rentals in Matagorda Country. We are dedicated to making your experience with us go as smoothly as possible. Fill out the form below or call us if you have any questions or concerns.",style: TextStyle(
                          color: Colors.white70,fontSize: 15,Text(
                        "Mobile No:",style: TextStyle(
                            color: Colors.white,fontWeight: FontWeight.w400),TextField(
                        style: style,maxLength: 30,cursorColor: Colors.red,textAlign: TextAlign.left,keyboardType: TextInputType.number,decoration: Inputdecoration(
                          hintText: 'Enter your mobile number',hintStyle:
                              TextStyle(fontSize: 16,color: Colors.white),border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(8),borderSide: BorderSide(
                              width: 0,style: BorderStyle.none,filled: true,fillColor: Color(0xFF1E1E1E),contentPadding: EdgeInsets.all(16),Text(
                        "Email:",maxLength: 16,keyboardType: TextInputType.emailAddress,decoration: Inputdecoration(
                          hintText: 'Enter your email',Text(
                        "Description:",TextField(
                        keyboardType: TextInputType.text,maxLines: 8,maxLength: 1000,style: style,decoration: Inputdecoration(
                          hintText: 'Enter Description Here',Material(
                        elevation: 5.0,borderRadius: BorderRadius.circular(30.0),color: Colors.red,child: MaterialButton(
                          minWidth: MediaQuery.of(context).size.width,padding: EdgeInsets.fromLTRB(20.0,15.0,20.0,15.0),onpressed: () {
//                        Route route =
//                        MaterialPageRoute(builder: (context) => Payment2());
//                        Navigator.pushReplacement(context,route);
                          },child: Text("Submit",style: style.copyWith(
                                  color: Colors.white,fontWeight: FontWeight.w800)),))
            ],bottomNavigationBar: BottomNavigation(),);
  }
}

视频链接 [问题视频] [1] [1]:https://drive.google.com/file/d/11i4R9DZaimO8CGw7TwOwQ0wbXXn9yHLr/view?usp=sharing

解决方法

在小部件构建之外定义警报对话框,并在需要时调用它

createAlertDialog(BuildContext){ 
return AlertDialog( //Your work ) 
@override   Widget build(BuildContext context) { 
//Whenver you need alertdialog call createAlertDialog()  }```
,

Flutter有一个showDialog()方法,特别是用于对话框小部件。您可以将AlertDialog小部件放入其中,并在showDialog()中调用initState()方法,当创建有状态小部件时,该方法将被完全调用一次,因此在重建期间不会被调用。如果要在点击其他区域时关闭对话框,请将barrierDismissible方法的showDialog()参数设置为true。

您只需要像这样修改_ContactState类:

class _ContactState extends State<Contact> {
        
     @override
      void initState() {
        super.initState();
        Future.delayed(
            Duration.zero,() => showDialog(
                  context: context,barrierDismissible: true,builder: (_) => AlertDialog(
                   // content

               ),)   
         );
       }
        
     @override
     Widget build(BuildContext context) {
       //your original code without the AlertDialog widget
     }
}

========================== 。更新

我在此代码中做了一些更改。现在一切正常

,

此现象是由于在构建方法中定义了警报对话框而引起的。每次重新构建窗口小部件或状态更改时,都会运行一次build方法,因此,将再次显示“警报”对话框。

尝试将AlertDialog对话框变量移至build方法之外。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...