有没有办法避免在 Flutter 中使用“null”来显示有条件的应用栏操作?

问题描述 投票:0回答:2

我想知道如果我有这种状态条件,是否有办法避免将操作设置为空:

appBar: AppBar(
      title: provider.appBarTitle,
      actions: provider.editState == EditState.inactive
          ? [
              IconButton(
                  onPressed: provider.activateEditableState,
                  icon: const Icon(Icons.edit))
            ]
          : null)
flutter dart appbar
2个回答
2
投票

如果你返回一个空列表而不是

null
应该没问题。

appBar: AppBar(
  title: provider.appBarTitle,
  actions: [
    if (provider.editState == EditState.inactive)
      IconButton(
        onPressed: provider.activateEditableState,
        icon: const Icon(Icons.edit),
      ),
  ],
),

0
投票
appBar: AppBar(
title: provider.appBarTitle,
actions:[ 
  provider.editState == EditState.inactive
  ? 
   IconButton(
   onPressed: provider.activateEditableState,
   icon: const Icon(Icons.edit))
  :
   // return a Container instead of null
   Container(),
  // other actions...
 ]
© www.soinside.com 2019 - 2024. All rights reserved.