Flutter-在可禁用小部件上禁用禁用方向

时间:2018-09-24 11:34:19

标签: android ios dart flutter

我正在编写一个应用程序来保存您的购物清单。那些项目对于编辑内容或删除项目应该是可忽略的。如果文本字段中已经有其他内容要编辑,则应禁用特定方向的滑动。现在,我的“可笑”看起来像这样。

    Dismissible(
    background: Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(left: 20.0),
      color: Colors.redAccent,
      child: Icon(Icons.delete, color: Colors.white),
    ),
    secondaryBackground: Container(
      alignment: Alignment.centerRight,
      padding: EdgeInsets.only(right: 20.0),
      color: Colors.orangeAccent,
      child: Icon(Icons.edit, color: Colors.white),
    ),
    child: Text("This is my dismissible widget"),
    onDismissed: (direction){
      if(direction == DismissDirection.startToEnd){
        Scaffold.of(context).showSnackBar(SnackBar(content: Text("Swipe to right")));
      } else if(direction == DismissDirection.endToStart){
        Scaffold.of(context).showSnackBar(SnackBar(content: Text("Swipe to right")));
      }
    },
  )

解决方案:

添加这样的撤退路线

    Dismissible(
    background: Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(left: 20.0),
      color: Colors.redAccent,
      child: Icon(Icons.delete, color: Colors.white),
    ),
    secondaryBackground: Container(
      alignment: Alignment.centerRight,
      padding: EdgeInsets.only(right: 20.0),
      color: Colors.orangeAccent,
      child: Icon(Icons.edit, color: Colors.white),
    ),
    child: Text("This is my dismissible widget"),
    /*New*/
    direction: (textController.text.isEmpty
        ? DismissDirection.horizontal
        : DismissDirection.startToEnd
    ),
    /*New*/
    onDismissed: (direction){
      if(direction == DismissDirection.startToEnd){
        Scaffold.of(context).showSnackBar(SnackBar(content: Text("Swipe to right")));
      } else if(direction == DismissDirection.endToStart){
        Scaffold.of(context).showSnackBar(SnackBar(content: Text("Swipe to right")));
      }
    },
  )

3 个答案:

答案 0 :(得分:0)

您可以通过使用TextItem填充列表而不是使用TextItem禁用Disableable来禁用对列表项的可撤消操作。

return new ListView(
     children: snapshot.data.documents.map((document) {
       // Convert snapshot to user item
       MyFBDocuments.ChatMessageItem documentData =
          new MyFBDocuments.ChatMessageItem.fromDocumentSnapshot(
              document);

       // One List Item 
       Widget listItem = new _ChatMessageItem(
           loading: false,
           openedGroupChat: chatInfoDoc.isGroupChat,
           messageDocument: documentData);

       // Determine if dismiss action should be shown 
       if (documentData.senderId == MyAuthentication.authenticatedUser.uid){
         // Create item with dismiss action 
         DismissDirection _dismissDirection = DismissDirection.startToEnd;

         return _DismissableListItem(
           messageDocData: documentData,
           listItem: listItem,
           onRightAction: _handleRightAction,
           onDelete: _handleLeftAction,
           dismissDirection: _dismissDirection,
         );

       } else {
         // display item without dismiss action 
         return listItem;
       }
     }).toList(),
     reverse: true,
     padding: new EdgeInsets.fromLTRB(8.0, 0.0, 8.0, 0.0),
   );

^代码来自CaughtUp:https://www.caughtup-app.com/

答案 1 :(得分:0)

要实现需要声明一个局部变量来操纵dismiss direction并根据您的条件设置其值的需求:

// Set required dismiss direction
DismissDirection dismissDirection;
if(OneConditionSatisfies && AnotherConditionSatisfies) {
  dismissDirection = DismissDirection.horizontal; // two side dismission
} else if(OneConditionSatisfies) {
  dismissDirection = DismissDirection.startToEnd; // only this one
} else if(AnotherConditionSatisfies) {
  dismissDirection = DismissDirection.endToStart; // only this one
}

// Return widget from `build` function
return Dismissible(
  ...
  direction: dismissDirection,
  ...
);

答案 2 :(得分:0)

你改变:onDismissed:(DismissDirection方向)

在 lambda 中:

onDismissed: (DismissDirection direction){

if(direction = DismissDirection.startToEnd) 
ect ....
}