通过在 AlertDialog 的子窗口小部件中使用 setstate 设置 AlertDialog 屏幕的状态

发布于 2025-01-11 14:41:41 字数 2617 浏览 0 评论 0原文

Future<void> ChangeProfile(BuildContext context) {
  final pro = Provider.of<Pro>(context, listen: false);
  FirebaseFirestore fireStore = FirebaseFirestore.instance;
  US(context);
  int count = 0;
  return showDialog<void>(
    context: context,
    builder: (context) {
      return StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
          return AlertDialog(
            // RoundedRectangleBorder - Dialog 화면 모서리 둥글게 조절
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0)),
            //Dialog Main Title
            title: Column(
              children: <Widget>[
                SizedBox(
                  width: 50.w,
                  height: 50.h,
                  child: Image.asset(
                    pro.currentProfile
                  ),
                )
              ],
            ),
            //
            content: SizedBox(
              width: 400.w,
              height: 400.h,
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      
                      PrintProfile(1,"imagename");

这是我的 AlertDialog 代码,我使用 StatefulBuilder

来更改 AlertDialog 内的屏幕以及

class PrintProfile extends StatefulWidget {
  PrintProfile(this.num,this.text);

  final int num;
  final String text;


  @override
  State<PrintProfile> createState() => _PrintProfileState();
}
class _PrintProfileState extends State<PrintProfile> {

  @override
  Widget build(BuildContext context) {
    ParentState parent = context.findAncestorStateOfType<ParentState>();
    final int num = widget.num;
    final String text = widget.text;
    final pro = Provider.of<Pro>(context);
    final double tophe = MediaQuery.of(context).padding.top;
    US(context);
    return SizedBox(
      width: 70.w,
      height: 70.h,
      child: OutlinedButton(
        onPressed: (){
          setState(() {
            pro.currentProfile= text;
          });
        },
        child: Image.asset(text),
      ),
    );
  }
}

PrintProfile 小部件。当从 AlertDialog 调用此小部件时,必须更改 AlertDialog 的输出部分 pro.currentProfile,并且必须设置父 AlertDialog setstate。

ParentState parent = context.findAncestorStateOfType<ParentState>();

我找到了这样的解决方案,但由于父窗口小部件是一个AlertDialog,我必须使用另一种方法。但我不知道。

问题摘要:当使用 StatefulBuilder 在 AlertDialog 中的子窗口小部件中按下按钮时,我想让 setstate 在父 AlertDialog 中运行。

Future<void> ChangeProfile(BuildContext context) {
  final pro = Provider.of<Pro>(context, listen: false);
  FirebaseFirestore fireStore = FirebaseFirestore.instance;
  US(context);
  int count = 0;
  return showDialog<void>(
    context: context,
    builder: (context) {
      return StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
          return AlertDialog(
            // RoundedRectangleBorder - Dialog 화면 모서리 둥글게 조절
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.0)),
            //Dialog Main Title
            title: Column(
              children: <Widget>[
                SizedBox(
                  width: 50.w,
                  height: 50.h,
                  child: Image.asset(
                    pro.currentProfile
                  ),
                )
              ],
            ),
            //
            content: SizedBox(
              width: 400.w,
              height: 400.h,
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      
                      PrintProfile(1,"imagename");

This is my AlertDialog code I used Statefu

lBuilder to change the screen inside the AlertDialog too

class PrintProfile extends StatefulWidget {
  PrintProfile(this.num,this.text);

  final int num;
  final String text;


  @override
  State<PrintProfile> createState() => _PrintProfileState();
}
class _PrintProfileState extends State<PrintProfile> {

  @override
  Widget build(BuildContext context) {
    ParentState parent = context.findAncestorStateOfType<ParentState>();
    final int num = widget.num;
    final String text = widget.text;
    final pro = Provider.of<Pro>(context);
    final double tophe = MediaQuery.of(context).padding.top;
    US(context);
    return SizedBox(
      width: 70.w,
      height: 70.h,
      child: OutlinedButton(
        onPressed: (){
          setState(() {
            pro.currentProfile= text;
          });
        },
        child: Image.asset(text),
      ),
    );
  }
}

PrintProfile widget. When this widget is called from AlertDialog, the output part of AlertDialog, pro.currentProfile, must be changed and the parent AlertDialog setstate must be set.

ParentState parent = context.findAncestorStateOfType<ParentState>();

I found such a solution, but since the parent widget is an AlertDialog, I have to use another method. But I don't know.

Summary of Question: I want to make the setstate run in the parent AlertDialog when a button is pressed in the child widget in the AlertDialog using StatefulBuilder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2025-01-18 14:41:41

这个类将为您提供使用当前类的父类 setstate 的明确答案,与您可以为 AlertDialog 类执行的方法相同

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'main1.dart';

void main() {
  runApp(MaterialApp(
    home: Modalbtn(),
  ));
}

class Modalbtn extends StatefulWidget {
  @override
  _ModalbtnState createState() => _ModalbtnState();
}

class _ModalbtnState extends State<Modalbtn> {
  String value = "0";
  // Pass this method to the child page.
  void _update(String newValue) {
    setState(() => value = newValue);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            IconButton(
              onPressed: () {
                showModalBottomSheet(
                    context: context,
                    builder: (BuildContext context) {
                      return Container(
                        height: 200,
                        child: Column(
                          children: [StatefulModalbtn(update: _update)],
                        ),
                      );
                    });
              },
              icon: Icon(Icons.add),
              iconSize: 20,
            ),
            Text(
              value,
              style: TextStyle(fontSize: 40),
            ),
          ],
        ),
      ),
    );
  }
}

import 'package:flutter/material.dart';

class StatefulModalbtn extends StatelessWidget {
  final ValueChanged<String> update;
  StatefulModalbtn({required this.update});

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () => update("100"), // Passing value to the parent widget.

      child: Text('Update (in child)'),
    );
  }
}

This class will give you clear answer for use parent class setstate from current class, the same method you can do for your AlertDialog class

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'main1.dart';

void main() {
  runApp(MaterialApp(
    home: Modalbtn(),
  ));
}

class Modalbtn extends StatefulWidget {
  @override
  _ModalbtnState createState() => _ModalbtnState();
}

class _ModalbtnState extends State<Modalbtn> {
  String value = "0";
  // Pass this method to the child page.
  void _update(String newValue) {
    setState(() => value = newValue);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            IconButton(
              onPressed: () {
                showModalBottomSheet(
                    context: context,
                    builder: (BuildContext context) {
                      return Container(
                        height: 200,
                        child: Column(
                          children: [StatefulModalbtn(update: _update)],
                        ),
                      );
                    });
              },
              icon: Icon(Icons.add),
              iconSize: 20,
            ),
            Text(
              value,
              style: TextStyle(fontSize: 40),
            ),
          ],
        ),
      ),
    );
  }
}

import 'package:flutter/material.dart';

class StatefulModalbtn extends StatelessWidget {
  final ValueChanged<String> update;
  StatefulModalbtn({required this.update});

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () => update("100"), // Passing value to the parent widget.

      child: Text('Update (in child)'),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文