flutter statefulWidget无法进行所需更改的问题

6ojccjat  于 2022-12-30  发布在  Flutter
关注(0)|答案(2)|浏览(189)

我正在开发一个statefulWidget,我的目的是确保next按钮在某个选项(在这种语言中)被选中之前是不可点击的。然而,它似乎并不起作用,我还在代码中添加了Yaesin(回答的人)的答案

ListView.builder(
            itemCount: histoires.length,
            itemBuilder: (context, index) {
              return ListTile(
                  title: Text(
                    histoires[index].title,
                    style: TextStyle(color: Colors.pink),
                  ),
                  trailing: IconButton(
                      icon: Icon(Icons.play_arrow),
                      onPressed: () {
                        showDialog(
                            context: context,
                            builder: (BuildContext context) {
                              return StatefulBuilder(
                                  builder: (context, setState) =>
                                      AlertDialog(
                                        content: Column(children: [
                                          InkWell(
                                              onTap: () {
                                                _handleTap;
                                              },
                                              child: ListTile(
                                                  trailing: Icon(Icons
                                                      .flag_circle_rounded),
                                                  title: Text(
                                                    "French",
                                                    style: TextStyle(
                                                        color: Colors
                                                            .blueGrey),
                                                  ))),
                                          _active
                                              ? InkWell(
                                                  onTap: () {},
                                                  child: Image.asset(
                                                      "assets/nextactive.png",
                                                      height: height * 0.2,
                                                      width: width * 0.4),
                                                )
                                              : Image.asset(
                                                  "assets/nextinactive.png",
                                                  height: height * 0,
                                                  width: width * 0)
                                        ]),
                                      ));
                            });
                      }));
            }),
gab6jxml

gab6jxml1#

由于您使用的是Dialog,因此要使setState正常工作,您需要用StatefulBuilder Package 它。
你还没有包括你的完整代码,所以我使用这个例子采取from the docs

await showDialog<void>(
  context: context,
  builder: (BuildContext context) {
    int? selectedRadio = 0;
    return AlertDialog(
      content: StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
          return Column(
            mainAxisSize: MainAxisSize.min,
            children: List<Widget>.generate(4, (int index) {
              return Radio<int>(
                value: index,
                groupValue: selectedRadio,
                onChanged: (int? value) {
                  setState(() => selectedRadio = value);
                },
              );
            }),
          );
        },
      ),
    );
  },
);

另请参见

Flutter团队的YouTube video解释StatefulBuilder

7gyucuyw

7gyucuyw2#

要更新对话框UI,可以使用StatefulBuilder的setState

return StatefulBuilder(
    builder: (context, setState) =>  
      AlertDialog(
          content: Column(children: [

有关使用StatefulBuilder的详细信息

相关问题