我无法更新UI或小部件,即使使用StatefulWidget。

huangapple go评论55阅读模式
英文:

I cant update the UI or widgets even with the StatefulWidget

问题

I want to update the height of the first alertDialog right after I press the "increase the height" in the secondDialog but it didn't change.

this is my code:

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  // this is the first value of the height
  double heightSize = 200;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: FloatingActionButton(
        onPressed: () async {
          setState(() {
            print('the size0 is $heightSize');

            showDialog(
              context: context,
              builder: (context) {
                return StatefulBuilder(
                  builder: (context, setState) {
                    return AlertDialog(
                      title: Container(
                        padding: EdgeInsets.only(
                            left: 10, right: 10, top: 5, bottom: 4),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              'Download File',
                              style: TextStyle(
                                  color: Color.fromARGB(255, 83, 82, 82),
                                  fontSize: 20),
                            ),
                          ],
                        ),
                      ),
                      content: Container(
                        // I want to change to 500 right after I press the button
                        height: heightSize,
                        child: Column(
                          children: [
                            IconButton(
                                onPressed: () {
                                  setState(() {
                                    print('the size1 is $heightSize');
                                    showDialog(
                                      context: context,
                                      builder: (context) {
                                        return StatefulBuilder(
                                          builder: (context, setState) {
                                            return AlertDialog(
                                              content: GestureDetector(
                                                child: Text(
                                                    'increase the height '),
                                                onTap: () {
                                                  setState(() {
                                                    // this is the button I want to change to 500
                                                    heightSize = 500;
                                                    print(
                                                        'the size2 is $heightSize');

                                                    Navigator.pop(context);
                                                  });
                                                },
                                              ),
                                            );
                                          },
                                        );
                                      },
                                    );
                                  });
                                },
                                icon: Icon(FontAwesomeIcons.paste))
                          ],
                        ),
                      ),
                    );
                  },
                );
              },
            );
          });
        },
      ),
    );
  }
}

if I close the two of the alert-dialog and press floating button it works, but I want to change it right after I press the "increase Size" button.

I tried using setState, but it didn't work.

英文:

I want to update the height of the first alertDialog right after I press the "increse the height" in the secoundDialog but it didn't change

this is my code:

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});
  @override
  State&lt;MyWidget&gt; createState() =&gt; _MyWidgetState();
}

class _MyWidgetState extends State&lt;MyWidget&gt; {
  // this is the firt value of the height
  double heightSize = 200;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: FloatingActionButton(
        onPressed: () async {
          setState(() {
            print(&#39;the size0 is $heightSize&#39;);

            showDialog(
              context: context,
              builder: (context) {
                return StatefulBuilder(
                  builder: (context, setState) {
                    return AlertDialog(
                      title: Container(
                        padding: EdgeInsets.only(
                            left: 10, right: 10, top: 5, bottom: 4),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              &#39;Download File&#39;,
                              style: TextStyle(
                                  color: Color.fromARGB(255, 83, 82, 82),
                                  fontSize: 20),
                            ),
                          ],
                        ),
                      ),
                      content: Container(
                        // I want to change to 500 right after I press the button
                        height: heightSize,
                        child: Column(
                          children: [
                            IconButton(
                                onPressed: () {
                                  setState(() {
                                    print(&#39;the size1 is $heightSize&#39;);
                                    showDialog(
                                      context: context,
                                      builder: (context) {
                                        return StatefulBuilder(
                                          builder: (context, setState) {
                                            return AlertDialog(
                                              content: GestureDetector(
                                                child: Text(
                                                    &#39;increase the height &#39;),
                                                onTap: () {
                                                  setState(() {
                                                    // this is the button I want to change to 500
                                                    heightSize = 500;
                                                    print(
                                                        &#39;the size2 is $heightSize&#39;);

                                                    Navigator.pop(context);
                                                  });
                                                },
                                              ),
                                            );
                                          },
                                        );
                                      },
                                    );
                                  });
                                },
                                icon: Icon(FontAwesomeIcons.paste))
                          ],
                        ),
                      ),
                    );
                  },
                );
              },
            );
          });
        },
      ),
    );
  }
}

if I close the two of the alert-dialog and press floating button it work but I want to change right after I press the increase Size button

I try the set state but it doesnt work

答案1

得分: 0

尝试使用异步方法,如下所示:

onPressed: () async {
  await showDialog(...);
  setState(() {});
  print('the size1 is $heightSize');
},
class MyWidget extends StatefulWidget {
  const MyWidget({super.key});
  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
 
  double heightSize = 200;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: FloatingActionButton(
        onPressed: () async {
          setState(() {
            print('the size0 is $heightSize');

            showDialog(
              context: context,
              builder: (context) {
                return StatefulBuilder(
                  builder: (context, setState) {
                    return AlertDialog(
                      title: Container(
                        padding: EdgeInsets.only(
                            left: 10, right: 10, top: 5, bottom: 4),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              'Download File',
                              style: TextStyle(
                                  color: Color.fromARGB(255, 83, 82, 82),
                                  fontSize: 20),
                            ),
                          ],
                        ),
                      ),
                      content: Container(
                        // I want to change to 500 right after I press the button
                        height: heightSize,
                        child: Column(
                          children: [
                            IconButton(
                                onPressed: () async {
                                  await showDialog(
                                    context: context,
                                    builder: (context) {
                                      return StatefulBuilder(
                                        builder: (context, setState) {
                                          return AlertDialog(
                                            content: GestureDetector(
                                              child:
                                                  Text('increase the height '),
                                              onTap: () {
                                                setState(() {
                                                  // this is the button I want to change to 500
                                                  heightSize = 500;
                                                  print(
                                                      'the size2 is $heightSize');

                                                  Navigator.pop(context);
                                                });
                                              },
                                            ),
                                          );
                                        },
                                      );
                                    },
                                  );

                                  setState(() {});
                                  print('the size1 is $heightSize');
                                },
                                icon: Icon(Icons.paste))
                          ],
                        ),
                      ),
                    );
                  },
                );
              },
            );
          });
        },
      ),
    );
  }
}
英文:

Try to use async method like

onPressed: () async {
  await showDialog(...);
  setState(() {});
  print(&#39;the size1 is $heightSize&#39;);
},

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});
  @override
  State&lt;MyWidget&gt; createState() =&gt; _MyWidgetState();
}

class _MyWidgetState extends State&lt;MyWidget&gt; {
 
  double heightSize = 200;
  @override
  Widget build(BuildContext context) {
    return Container(
      child: FloatingActionButton(
        onPressed: () async {
          setState(() {
            print(&#39;the size0 is $heightSize&#39;);

            showDialog(
              context: context,
              builder: (context) {
                return StatefulBuilder(
                  builder: (context, setState) {
                    return AlertDialog(
                      title: Container(
                        padding: EdgeInsets.only(
                            left: 10, right: 10, top: 5, bottom: 4),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              &#39;Download File&#39;,
                              style: TextStyle(
                                  color: Color.fromARGB(255, 83, 82, 82),
                                  fontSize: 20),
                            ),
                          ],
                        ),
                      ),
                      content: Container(
                        // I want to change to 500 right after I press the button
                        height: heightSize,
                        child: Column(
                          children: [
                            IconButton(
                                onPressed: () async {
                                  await showDialog(
                                    context: context,
                                    builder: (context) {
                                      return StatefulBuilder(
                                        builder: (context, setState) {
                                          return AlertDialog(
                                            content: GestureDetector(
                                              child:
                                                  Text(&#39;increase the height &#39;),
                                              onTap: () {
                                                setState(() {
                                                  // this is the button I want to change to 500
                                                  heightSize = 500;
                                                  print(
                                                      &#39;the size2 is $heightSize&#39;);

                                                  Navigator.pop(context);
                                                });
                                              },
                                            ),
                                          );
                                        },
                                      );
                                    },
                                  );

                                  setState(() {});
                                  print(&#39;the size1 is $heightSize&#39;);
                                },
                                icon: Icon(Icons.paste))
                          ],
                        ),
                      ),
                    );
                  },
                );
              },
            );
          });
        },
      ),
    );
  }
}

huangapple
  • 本文由 发表于 2023年2月18日 23:35:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定