android 按下任意按钮时,Flutter重新打开警报对话框

n9vozmp4  于 2022-11-03  发布在  Android
关注(0)|答案(1)|浏览(153)

当我导航到该屏幕时,它会从_navigateSlide小部件打开showDialog box,并且有onTap功能和一个提交按钮,但当我按下该按钮时,它会重新打开_navigateSlide,我不知道为什么每次单击时它都会重新打开_navigateSlide。我在每个onTap上添加Navigator.pop(context, 'Cancel');,还在Apply按钮提交上添加Navigator.pop(context, 'Cancel');
请帮忙解决这个问题。
这是我的代码:-

class ExploreAds extends StatefulWidget {

ExploreAds({Key? key}) : super(key: key);

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

class _ExploreAds extends State<ExploreAds> {

bool show = false;
bool check1 = false;
bool check2 = false;
bool check3 = false;
bool check4 = false;

final List<String> data = <String>[];

void addValue(txt) {
 setState(() {
  data.add('${txt}');
});
print(data);

}

void removeValue(rmtxt) {

setState(() {
  data.remove('${rmtxt}');
});
print(data);

}

@override
_navigateSlide(BuildContext context) {
final Size size = MediaQuery.of(context).size;
showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
          content: Container(
            height: 550,
            child: 
            Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                addVerticalSpace(10),
                Expanded(
                  child : Container(
                    child: GridView(
                        padding: EdgeInsets.only(right:8,),
                        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2,
                          crossAxisSpacing: 20,
                          mainAxisSpacing: 20,
                          childAspectRatio: 0.70,
                        ),
                        children: <Widget>[
                          Stack(
                            children: [
                              Container(
                                padding: EdgeInsets.only(right:5, left:5),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(10),
                                  border: Border.all(color: Color(0xffacacac)),
                                ),
                                child: InkWell(
                                    onTap: () => setState(
                                          () {
                                        check1 = !check1;
                                        check1? addValue('Washing machine') : removeValue('Washing machine');
                                        Navigator.pop(context, 'Cancel');
                                      },
                                    ),
                                    child: Column(
                                      children: [
                                        Image.asset('assets/icons/pngwing44.png', width: 115, height: 115),
                                        Text('Washing machine')
                                      ],
                                    )
                                ),
                              ),
                              Visibility(
                                  visible: check1 ? true : false,
                                  child: Positioned(
                                    top: 5,
                                    right: 5,
                                    child: Image.asset('assets/icons/tick.png', width: 15,),
                                  )
                              )
                            ],
                          ),
                          Stack(
                            children: [
                              Container(
                                padding: EdgeInsets.only(right:5, left:5),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(10),
                                  border: Border.all(color: Color(0xffacacac)),
                                ),
                                child: InkWell(
                                    onTap: () => setState(
                                          () {
                                        check2 = !check2;
                                        check2? addValue('Air Conditioner') : removeValue('Air Conditioner');
                                        Navigator.pop(context, 'Cancel');
                                      },
                                    ),
                                    child: Column(
                                      children: [
                                        Image.asset('assets/icons/pngwing45.png', width: 115, height: 115),
                                        Text('Air Conditioner')
                                      ],
                                    )
                                ),
                              ),
                              Visibility(
                                  visible: check2 ? true : false,
                                  child: Positioned(
                                    top: 5,
                                    right: 5,
                                    child: Image.asset('assets/icons/tick.png', width: 15,),
                                  )
                              )
                            ],
                          ),
                          Stack(
                            children: [
                              Container(
                                padding: EdgeInsets.only(right:5, left:5),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(10),
                                  border: Border.all(color: Color(0xffacacac)),
                                ),
                                child: InkWell(
                                    onTap: () => setState(
                                          () {
                                        check3 = !check3;
                                        check3? addValue('Microwave') : removeValue('Microwave');
                                        Navigator.pop(context, 'Cancel');
                                      },
                                    ),
                                    child: Column(
                                      children: [
                                        Image.asset('assets/icons/pngwing45.png', width: 115, height: 115),
                                        Text('Microwave')
                                      ],
                                    )
                                ),
                              ),
                              Visibility(
                                  visible: check3 ? true : false,
                                  child: Positioned(
                                    top: 5,
                                    right: 5,
                                    child: Image.asset('assets/icons/tick.png', width: 15,),
                                  )
                              )
                            ],
                          ),
                          Stack(
                            children: [
                              Container(
                                padding: EdgeInsets.only(right:5, left:5),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(10),
                                  border: Border.all(color: Color(0xffacacac)),
                                ),
                                child: InkWell(
                                    onTap: () => setState(
                                          () {
                                        check4 = !check4;
                                        check4? addValue('Refrigerators') : removeValue('Refrigerators');
                                        Navigator.pop(context, 'Cancel');
                                      },
                                    ),
                                    child: Column(
                                      children: [
                                        Image.asset('assets/icons/pngwing46.png', width: 115, height: 115),
                                        Text('Refrigerators')
                                      ],
                                    )
                                ),
                              ),
                              Visibility(
                                  visible: check4 ? true : false,
                                  child: Positioned(
                                    top: 5,
                                    right: 5,
                                    child: Image.asset('assets/icons/tick.png', width: 15,),
                                  )
                              )
                            ],
                          ),
                        ]
                    ),
                  ),
                ),
                addVerticalSpace(10),
                Padding(
                  padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
                  child: ElevatedButton(
                    onPressed: () => Navigator.pop(context, 'Cancel'),
                    style: ElevatedButton.styleFrom(
                      elevation: 6,
                      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(80.0)),
                      padding: const EdgeInsets.all(0.0),
                    ),
                    child: Container(
                      constraints: const BoxConstraints(minWidth: 88.0, minHeight: 50.0), // min sizes for Material buttons
                      alignment: Alignment.center,
                      child: const Text(
                        'Apply',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 20,
                        ),
                      ),
                    ),
                  ),
                )
              ],
            )
          )
      );
    });
 }

 @override
 Widget build(BuildContext context) {
  final Size size = MediaQuery.of(context).size;
  final ThemeData themeData = Theme.of(context);
  const double padding = 25;
  const sidePadding = EdgeInsets.symmetric(horizontal: padding);
  Future.delayed(Duration.zero, () => _navigateSlide(context));

 return Scaffold(
  body: Container(
      padding: sidePadding,
      width: size.width,
      height: size.height,
      child: Text('$data')
  ),
);
}

}

_navigateSlide在每个onTaponPressed上打开请帮助解决此问题。

3pmvbmvn

3pmvbmvn1#

您的问题在此代码中:

Future.delayed(Duration.zero, () => _navigateSlide(context));

因为它位于被多次调用的build方法中。
将代码移到initState内,例如:

@override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _navigateSlide(context);
    });
  }

方法WidgetsBinding.instance.addPostFrameCallback在构建方法完成后调用其回调

相关问题