滑动时可显示底部组件,可指定滑动的方向和交叉轴的偏移量。接收确认消失和消失时的回调。

相关组件

ListView

Dismissible基本使用

【child】 : 子组件 【Widget】
【background】 : 左底 【Widget】
【secondaryBackground】 : 右底 【Widget】
【key】 : 键 【Key】
【confirmDismiss】 : 确认回调 【DismissDirectionCallback】
【onDismissed】 : 消失回调 【DismissDirectionCallback】,
113.gif

  1. import 'package:flutter/material.dart';
  2. class CustomDismissible extends StatefulWidget {
  3. @override
  4. _CustomDismissibleState createState() => _CustomDismissibleState();
  5. }
  6. class _CustomDismissibleState extends State<CustomDismissible> {
  7. var data = <Color>[
  8. Colors.purple[50],
  9. Colors.purple[100],
  10. Colors.purple[200],
  11. Colors.purple[300],
  12. Colors.purple[400],
  13. Colors.purple[500],
  14. Colors.purple[600],
  15. Colors.purple[700],
  16. Colors.purple[800],
  17. Colors.purple[900],
  18. ];
  19. @override
  20. Widget build(BuildContext context) {
  21. return Container(
  22. height: 200,
  23. child: ListView(
  24. padding: EdgeInsets.symmetric(horizontal: 5),
  25. children: data.map((color) => _buildItem(color)).toList(),
  26. ),
  27. );
  28. }
  29. Widget _buildItem(Color color) {
  30. return Dismissible(
  31. background: Container(
  32. color: Colors.green,
  33. alignment: Alignment(-0.9, 0),
  34. child: Icon(
  35. Icons.check,
  36. color: Colors.white,
  37. ),
  38. ),
  39. secondaryBackground: Container(
  40. alignment: Alignment(0.9, 0),
  41. child: Icon(
  42. Icons.close,
  43. color: Colors.white,
  44. ),
  45. color: Colors.red,
  46. ),
  47. key: ValueKey(color),
  48. onDismissed: (d) {
  49. data.remove(color);
  50. },
  51. confirmDismiss: (e) async {
  52. if (e == DismissDirection.endToStart) {
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. },
  58. child: Container(
  59. alignment: Alignment.center,
  60. height: 50,
  61. color: color,
  62. child: Text(
  63. colorString(color),
  64. style: TextStyle(color: Colors.white, shadows: [
  65. Shadow(color: Colors.black, offset: Offset(.5, .5), blurRadius: 2)
  66. ]),
  67. ),
  68. ),
  69. );
  70. }
  71. String colorString(Color color) =>
  72. "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
  73. }

Dismissible基本使用

   <br />【direction】 : 方向   【DismissDirection】<br />【crossAxisEndOffset】 : 偏移  【double】,<br />![114.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589450165806-ad316baa-acf0-4496-8e3b-4c5c4d98eb8e.gif#align=left&display=inline&height=227&margin=%5Bobject%20Object%5D&name=114.gif&originHeight=227&originWidth=403&size=938350&status=done&style=none&width=403)
import 'package:flutter/material.dart';
class DirectionDismissible extends StatefulWidget {
  @override
  _CustomDirectionDismissibleState createState() => _CustomDirectionDismissibleState();
}

class _CustomDirectionDismissibleState extends State<DirectionDismissible> {
  var data = <Color>[
    Colors.purple[50],
    Colors.purple[100],
    Colors.purple[200],
    Colors.purple[300],
    Colors.purple[400],
    Colors.purple[500],
    Colors.purple[600],
    Colors.purple[700],
    Colors.purple[800],
    Colors.purple[900],
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child: ListView(
        scrollDirection: Axis.horizontal,
        padding: EdgeInsets.symmetric(horizontal: 5),
        children: data.map((color) => _buildItem(color)).toList(),
      ),
    );
  }

  Widget _buildItem(Color color) {
    return Dismissible(
      direction: DismissDirection.vertical,
      background: Container(
        color: Colors.green,
        alignment: Alignment( 0,-0.9,),
        child: Icon(
          Icons.check,
          color: Colors.white,
        ),
      ),
      crossAxisEndOffset: 0.5,
      secondaryBackground: Container(
        alignment: Alignment( 0,0.9,),
        child: Icon(
          Icons.close,
          color: Colors.white,
        ),
        color: Colors.red,
      ),
      key: ValueKey(color),
      onDismissed: (d) {
        data.remove(color);
      },
      confirmDismiss: (e) async {
        print(e);
        if (e == DismissDirection.up) {
          return true;
        } else {
          return false;
        }
      },
      child: Container(
        alignment: Alignment.center,
        width: 80,
        color: color,
        child: Text(
          colorString(color),
          style: TextStyle(color: Colors.white, shadows: [
            Shadow(color: Colors.black, offset: Offset(.5, .5), blurRadius: 2)
          ]),
        ),
      ),
    );
  }

  String colorString(Color color) =>
      "#${color.value.toRadixString(16).padLeft(8, '0').toUpperCase()}";
}