Navigator.push 可以和 Flutter 三元一起使用吗

Can Navigator.push be used in ternary with Flutter

我有一个包含四个选项卡的应用栏的页面。在其中一个选项卡上,我试图在构建方法中使用一个条件,以在条件为真时显示具有特定内容的自定义滚动视图。如果条件为假,我希望用户被导航到一个全新的页面,该页面只有一个列表视图和它自己的不包含任何选项卡的应用程序栏。 Navigator.push 被用作三元中的第二个条件会引发错误。我在下面代码中的目标类似于 if file == null 在 Safe Area 小部件中显示内容,否则导航到 UploadItemsFormPage。我一直无法弄清楚如何解决这个挑战。我试图删除很多代码以减少阅读量,但如果需要帮助,我可以提供更多代码。代码将被重构为不同的小部件,但我认为这会使问这个问题变得更加复杂,所以现在它全部集中在一个 class 中。在此先感谢您的帮助。

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 的任何真实条件)?(如果文件为 null 则做一些工作):Navigator.push( context, MaterialPageRoute(builder: (context) => Uploadimages()), ); (如果三元条件不正确,请进行更改)