home_page.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_slidable/flutter_slidable.dart';
  3. import 'package:flutter_slidable_demo/models/user_model.dart';
  4. /// Description: home page
  5. /// Time : 05/26/2023 Friday
  6. /// Author : liuyuqi.gov@msn.cn
  7. class HomePage extends StatefulWidget {
  8. const HomePage({super.key});
  9. @override
  10. State<HomePage> createState() => _HomePageState();
  11. }
  12. class _HomePageState extends State<HomePage> {
  13. List<UserModel> userList = [
  14. UserModel(id: 1, name: "User 1", email: "aa@qq.com", password: "123456"),
  15. UserModel(id: 2, name: "User 2", email: "bb@qq.com", password: "123456")
  16. ];
  17. @override
  18. Widget build(BuildContext context) {
  19. return Scaffold(
  20. appBar: AppBar(
  21. title: const Text("Slidable Demo"),
  22. ),
  23. body: ListView.builder(
  24. itemBuilder: _itemBuilder, itemCount: userList.length));
  25. }
  26. Widget _itemBuilder(BuildContext context, int index) {
  27. return Container(
  28. margin: const EdgeInsets.all(5.0),
  29. child: Slidable(
  30. key: Key(userList[index].toString()),
  31. startActionPane: const ActionPane(
  32. openThreshold: 0.1,
  33. closeThreshold: 0.4,
  34. motion: BehindMotion(),
  35. children: [
  36. SlideAction(color: Colors.green, icon: Icons.share),
  37. SlideAction(color: Colors.amber, icon: Icons.delete),
  38. ],
  39. ),
  40. endActionPane: const ActionPane(
  41. motion: BehindMotion(),
  42. children: [
  43. SlideAction(color: Colors.red, icon: Icons.delete_forever),
  44. SlideAction(color: Colors.blue, icon: Icons.alarm, flex: 2),
  45. ],
  46. ),
  47. child: Container(
  48. height: 80,
  49. width: double.infinity,
  50. color: Colors.blue,
  51. alignment: Alignment.center,
  52. child: Text(
  53. "product${userList[index].name}",
  54. style: const TextStyle(color: Colors.white),
  55. ),
  56. ),
  57. ),
  58. );
  59. }
  60. /// 弹出对话框
  61. showToast(String s) {
  62. showDialog(
  63. context: context,
  64. builder: (context) {
  65. return AlertDialog(
  66. content: Text(s),
  67. );
  68. });
  69. }
  70. }
  71. class SlideAction extends StatelessWidget {
  72. const SlideAction({
  73. Key? key,
  74. required this.color,
  75. required this.icon,
  76. this.flex = 1,
  77. }) : super(key: key);
  78. final Color color;
  79. final IconData icon;
  80. final int flex;
  81. @override
  82. Widget build(BuildContext context) {
  83. return SlidableAction(
  84. flex: flex,
  85. backgroundColor: color,
  86. foregroundColor: Colors.white,
  87. onPressed: (_) {
  88. print(icon);
  89. },
  90. icon: icon,
  91. label: 'hello',
  92. );
  93. }
  94. }