我有一个带有应用程序栏的页面,该应用程序栏有四个选项卡。在其中一个选项卡上,我试图使用构建方法中的一个条件来显示具有特定内容的自定义滚动视图(如果条件为true(。如果条件为false,我希望用户导航到一个全新的页面,该页面只有列表视图和自己的应用程序栏,其中不包含任何选项卡。Navigator.push被用作三进制中的第二个条件,会抛出一个错误。我在下面代码中的目标类似于如果file==null在安全区域小部件中显示内容,则导航到UploadItemsFormPage。我一直想不出如何解决这个挑战。我试着删除了很多代码,以减少阅读量,但如果有必要的话,我可以提供更多的代码。代码将被重构到不同的小部件中,但我认为这会使问这个问题变得更加复杂,所以现在它都在一个类中。提前感谢您的帮助。
class ShoppingAdminPage extends StatefulWidget {
const ShoppingAdminPage({Key? key}) : super(key: key);
@override
State<ShoppingAdminPage> createState() => _ShoppingAdminPageState();
}
class _ShoppingAdminPageState extends State<ShoppingAdminPage> {
final List<String> _tabs = <String>[
TabNameString.upload,
'Tab Two',
'Tab Three',
'Tab Four',
];
TextEditingController descriptionController = TextEditingController();
String productID = DateTime.now().microsecondsSinceEpoch.toString();
TextEditingController priceController = TextEditingController();
TextEditingController titleController = TextEditingController();
bool isUploading = false;
File? file;
void clearImage() {
setState(() {
file = null;
});
}
@override
Widget build(BuildContext context) {
return AdaptiveLayoutScaffold(
drawer: const SideSheet(),
landscapeBodyWidget: Container(),
portraitBodyWidget: BrandTabController(
actions: const [
BrandPopUpMenu(),
],
numberOfTabs: _tabs.length,
pageName: PageName.shoppingAdmin,
tabBarView: TabBarView(children: [
file == null
? SafeArea(
top: false,
bottom: false,
child: Builder(
builder: (BuildContext context) {
return CustomScrollView(
key: const PageStorageKey<String>(
TabNameString.upload,
),
slivers: <Widget>[
SliverOverlapInjector(
handle:
NestedScrollView.sliverOverlapAbsorberHandleFor(
context,
),
),
SliverToBoxAdapter(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(
height: 60.0,
),
SvgPicture.asset(
ImageUrlString.uploadItemSVG,
height: 260.0,
),
Padding(
padding: const EdgeInsets.only(
top: 20.0,
),
child: ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return BrandSimpleDialog(
dialogTitle:
DialogString.itemImage,
optionOneCallback: () async {
Navigator.pop(context);
XFile? pickedFile =
await ImagePicker()
.pickImage(
imageQuality: 85,
maxHeight: 675,
maxWidth: 960,
source: ImageSource.camera,
);
setState(() {
file = File(pickedFile!.path);
});
},
optionOneText: DialogString
.captureWithCamera,
optionTwoCallback: () async {
Navigator.pop(context);
XFile? pickedFile =
await ImagePicker()
.pickImage(
imageQuality: 85,
source: ImageSource.gallery,
);
setState(() {
file = File(pickedFile!.path);
});
},
optionTwoText: DialogString
.selectFromGallery,
);
});
},
child: Text(
ButtonString.uploadNewItems.toUpperCase(),
),
),
),
],
),
),
],
);
},
),
)
: UploadItemsFormPage(
contentImage: file as File,
descriptionController: descriptionController,
isUploading: isUploading,
onPressedClear: clearImage,
priceController: priceController,
titleController: titleController,
),
Container(),
Container(),
Container(),
]),
tabs: _tabs,
),
);
}
}
在任何情况下都可以使用三元运算符。假设在上述情况下(任何真正的编码,例如file==null(?(如果文件为空,请执行一些操作(:Navigator.push(context,MaterialPageRoute(builder:(context(=>上传图像((,(;(如果三元编码不正确,请更改工作(