简体   繁体   English

setstate 不适用于 showdialog 和 alertdialog

[英]setstate doesn't work with showdialog and alertdialog

Using a stateful widget and setstate with showdialog and alertdialog seems to not work properly in my code but for simplification i simulated the same issue with smaller and simpler code here for more problem understanding在我的代码中使用有状态小部件和setstateshowdialogalertdialog似乎无法正常工作,但为了简化起见,我在这里用更小更简单的代码模拟了同样的问题,以便更好地理解问题

import 'package:flutter/material.dart';
import '../widgets/app_drawer.dart';

class Test extends StatefulWidget {

  @override
  State<Test> createState() => _TestState();
}

class _TestState extends State<Test> {

  int x = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: AppDrawer(),
      appBar: AppBar(
        title: Text("Shop"),
      ),
      body: Column(
        children: [
          RaisedButton(
            onPressed: () {
              showDialog(
                context: context,
                builder: (ctx) => AlertDialog(
                  title: Text("hey"),
                  content: Text(x.toString()),
                  actions: [
                    RaisedButton(
                      onPressed: () {},
                      child: Text("click me"),
                    ),
                    RaisedButton(
                      onPressed: () {
                        setState(() {
                          x++;
                        });
                      },
                      child: Text("increment"),
                    )
                  ],
                ),
              );
            },
            child: Text("Click"),
          ),
          Text(x.toString()),
        ],
      ),
    );
  }
}

if you run this piece of code you will notice the number increasing in the background but not on the popup dialog and to see the change you have to close the dialog and reopen it to see the latest value the actual code (the not simplified version) is based on the same idea but a little different如果您运行这段代码,您会注意到数字在后台增加,但在弹出对话框中没有增加,要查看更改,您必须关闭对话框并重新打开它以查看实际代码的最新值(非简化版本)基于相同的想法但有点不同

the idea is based on a shop order where you see your cart with products and when you click on edit cart products a popup dialog appears with the previous values for the products and can remove or add products from the cart and click on Update button.这个想法基于一个商店订单,您可以在其中看到带有产品的购物车,当您单击编辑购物车产品时,会出现一个带有产品先前值的弹出对话框,您可以从购物车中删除或添加产品,然后单击更新按钮。 the onPressed value for the button can be a function which approves the new cart if the products list is not null and can be null (disabled button) if you removed all products from the cart but it doesn't seem to work immediately.如果产品列表不为空,按钮的onPressed值可以是一个批准新购物车的功能,如果您从购物车中删除了所有产品但它似乎不能立即工作,则可以为空(禁用按钮)。 the button looks something like this按钮看起来像这样

RaisedButton(
  onPressed: orderCart.isEmpty
  ? null
  : () {
       //Function here
    },
  child: Text(
       "Update",
      ),
  ),

You should use StatefulBuilder in showDialog to use setState((){});你应该在showDialog使用StatefulBuilder来使用 setState((){});

...
body: Column(
  children: [
    RaisedButton(
      onPressed: () {
        showDialog(
          context: context,
          builder: (BuildContext context) {
            return StatefulBuilder(
              builder: (context, StateSetter setState) {
                return AlertDialog( 
...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM