Flutter 如何将小部件函数中的信息返回到另一个页面?
我目前正在与一个团队合作,在那里我实现了一个条形码扫描仪,它将成为“搜索”页面上的一个小部件。我目前面临的问题是,我使用条形码生成一个我希望包含在搜索中的成分列表,但我不知道如何将该信息返回到搜索页面。到目前为止,我已经尝试了两种方法:Flutter 如何将小部件函数中的信息返回到另一个页面?,flutter,dart,Flutter,Dart,我目前正在与一个团队合作,在那里我实现了一个条形码扫描仪,它将成为“搜索”页面上的一个小部件。我目前面临的问题是,我使用条形码生成一个我希望包含在搜索中的成分列表,但我不知道如何将该信息返回到搜索页面。到目前为止,我已经尝试了两种方法: 我尝试创建scanner类的一个成员变量,需要时可以在搜索页面上访问该变量,但因为返回列表的函数是private state类的一部分,我不确定如何从public类访问它。这是我想要解决的方法 我曾尝试使用Navigator类从单独的屏幕中推送和弹出信息,但问题
Future<void> scanBarcodeNormal(BuildContext context) async {
String barcodeScanRes;
List<String> test;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
barcodeScanRes = await FlutterBarcodeScanner.scanBarcode(
"#ff6666", "Cancel", true, ScanMode.BARCODE);
} on PlatformException {
barcodeScanRes = 'Failed to get platform version.';
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
if (!mounted) return null;
//Call the backend with the barcode to return the Bread Crumb list
List<String> breadCrumbs = await BackendRequest.barcode("089836187635", "42e96d88b6684215c9e260273b5e56b0522de18e");
//If the backend does not return us anything this displays a popup
if(breadCrumbs == null){
showDialog(
context: context,
builder: (BuildContext context) => CustomDialog(
title: "Uh Oh",
description:
"Barcode not found in our database, please try entering the item manually",
buttonText: "Okay",
),
);
}
else{
setState(() {
_scanBarcode = breadCrumbs.toString();
});
//Check the breadcrumbs for usable ingredients
test = await getIngredients(breadCrumbs, "42e96d88b6684215c9e260273b5e56b0522de18e");
}
setState(() {
_itemName = test[0].toString();
});
//Navigator.pop(context, test);
您可以使用“提供消费者”模式,在该模式中,您可以将所有页面放入一个父主题子主题,然后可以访问变量和所有其他内容 其他方面,你也可以使用块模式
或者,如果您不想在这种情况下使用它,您可以创建一个公共类,并在其中声明一个静态变量,并在扫描完成后在结果上设置变量,并在需要时使用。您可以使用“提供用户模式”,在该模式中,您可以将所有页面放入一个父主题子级,然后您可以访问变量和所有其他变量东西 其他方面,你也可以使用块模式 或者,如果您不想在这种情况下使用它,您可以创建一个公共类,并在其中声明一个静态变量,在扫描完成后在结果上设置变量,并在需要时使用
decoration: InputDecoration(
labelText: "Input an Ingredient",
hintText: "Search",
prefixIcon: Icon(Icons.search),
suffixIcon: ScanButton(),