首页 > 解决方案 > How to pass data from stateful widget to Dialog Box?

问题描述

I want to pass the input taken from a user in stateful widget to the stateless DialogBox. I am a beginner and it is my first code.

the input will be taken in texfield in stateful widget and output will be displayed in Dialogbox

标签: androidflutterdart

解决方案


嗯...请尝试阅读文档(Dart)

https://dart.dev/guides

Flutter 是用 Dart 编写的,所以这就是我要开始的地方。

现在,回答您的问题,对于您的情况,为了将信息传递给无状态对话框,最简单的方法是使用构造函数。然后,当您调用 Dialogue Widget 时,您只需将此信息直接传递给它。

像这样:

class StatelessDialogue extends StatelessWidget{

final String informationToPass;
StatelessDialogue({@required this.informationToPass});

@override
Widget build(BuildContext context){
 return AlertDialog(
      contentPadding: EdgeInsets.all(20),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
      content: Text(this.informationToPass)
      );
 }
}

@required是 Dart 中的一个属性,用于向 IDE 指示该参数informationToPass是必需的,以便对话小部件工作。


推荐阅读