Advertisement
k4ilham

slideble list

Jul 11th, 2021
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.45 KB | None | 0 0
  1.  
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_slidable/flutter_slidable.dart';
  4. import 'package:ui_flutter/core/res/utils.dart';
  5.  
  6.  
  7.  
  8. ///This class  uses Slidable List library
  9. /// link to : https://pub.dev/packages/flutter_slidable
  10.  
  11. class SlidableList extends StatefulWidget {
  12.   static const routeName = '/SlidableList';
  13.   @override
  14.   _SlidableListState createState() => _SlidableListState();
  15. }
  16.  
  17. class _SlidableListState extends State<SlidableList> {
  18.   SlidableController slidableController;
  19.   List<_ListItem> listItems;
  20.  
  21.   void initListItems() {
  22.     int newIndex;
  23.     listItems = List<_ListItem>.generate(16, (int index) {
  24.       newIndex = index + 1;
  25.  
  26.       /// ---------
  27.       /// list item with all required data .
  28.       /// ---------
  29.       return _ListItem(
  30.         index: index,
  31.         initials: 'assets/images/01.png',
  32.         name: 'Item $index Sender',
  33.         subject: 'Subject: $index',
  34.         body: "[$index] first line of the message's body...",
  35.       );
  36.     });
  37.   }
  38.  
  39.   @override
  40.   void initState() {
  41.     super.initState();
  42.     slidableController = new SlidableController(
  43.       onSlideAnimationChanged: handleSlideAnimationChanged,
  44.       onSlideIsOpenChanged: handleSlideIsOpenChanged,
  45.     );
  46.     initListItems();
  47.   }
  48.  
  49.   Animation<double> _rotationAnimation;
  50.   Color _fabColor = Colors.blue;
  51.  
  52.   void handleSlideAnimationChanged(Animation<double> slideAnimation) {
  53.     setState(() {
  54.       _rotationAnimation = slideAnimation;
  55.     });
  56.   }
  57.  
  58.   void handleSlideIsOpenChanged(bool isOpen) {
  59.     setState(() {
  60.       _fabColor = isOpen ? utils.getThem().progress4: utils.getThem().progress6;
  61.     });
  62.   }
  63.  
  64.   @override
  65.   Widget build(BuildContext context) {
  66.     final title = 'Slidable List';
  67.     return new Scaffold(
  68.       backgroundColor: Colors.transparent,
  69.       appBar: new AppBar(
  70.         title: new Text(title),
  71.       ),
  72.       body: Container(
  73.         decoration: new BoxDecoration(
  74.           image: new DecorationImage(
  75.             colorFilter: new ColorFilter.mode(
  76.               Colors.black.withOpacity(0.4),
  77.               BlendMode.darken,
  78.             ),
  79.             image: new AssetImage("assets/images/03.png"),
  80.             fit: BoxFit.cover,
  81.           ),
  82.         ),
  83.         child: new Center(
  84.           ///  OrientationBuilder for Orientation side (top  or down )
  85.           child: new OrientationBuilder(
  86.  
  87.             builder: (context, orientation) => _buildList(
  88.                 context,
  89.                 orientation == Orientation.portrait
  90.                     ? Axis.vertical
  91.                     : Axis.horizontal),
  92.  
  93.           ),
  94.         ),
  95.       ),
  96.     );
  97.   }
  98.  
  99.   Widget _buildList(BuildContext context, Axis direction) {
  100.     return new ListView.builder(
  101.  
  102.       scrollDirection: direction,
  103.       itemBuilder: (context, index) {
  104.         final Axis slidableDirection =
  105.         direction == Axis.horizontal ? Axis.vertical : Axis.horizontal;
  106.         return _getSlidableWithLists(context, index, slidableDirection);
  107.       },
  108.       itemCount: listItems.length,
  109.     );
  110.   }
  111.  
  112.   Widget _buildRowItem(int index) {
  113.     final _ListItem item = listItems[index];
  114.     return new Row(
  115.       crossAxisAlignment: CrossAxisAlignment.center,
  116.       children: <Widget>[
  117.         new Container(
  118.           padding: const EdgeInsets.only(top: 16.0, bottom: 16.0),
  119.           margin: const EdgeInsets.only(left: 16.0, right: 16.0),
  120.           child: new CircleAvatar(
  121.             radius: 30.0,
  122.             backgroundImage: new AssetImage(item.initials),
  123.           ),
  124.         ),
  125.         new Column(
  126.           mainAxisAlignment: MainAxisAlignment.center,
  127.           crossAxisAlignment: CrossAxisAlignment.start,
  128.           children: <Widget>[
  129.             new Text(item.name,
  130.                 style: new TextStyle(
  131.                     fontSize: 18.0,
  132.                     color: Colors.white,
  133.                     fontWeight: FontWeight.bold)),
  134.             new Container(
  135.               margin: const EdgeInsets.only(top: 5.0),
  136.               child: new Text(
  137.                 item.subject,
  138.                 style: new TextStyle(fontSize: 16.0, color: Colors.white),
  139.               ),
  140.             )
  141.           ],
  142.         )
  143.       ],
  144.     );
  145.   }
  146.  
  147.   Widget _getSlidableWithLists(
  148.       BuildContext context, int index, Axis direction) {
  149.     final _ListItem item = listItems[index];
  150.     return Container(
  151.       margin: const EdgeInsets.only(top: 10.0, right: 5.0, left: 5.0),
  152.       child: new Card(
  153.         elevation: 4.0,
  154.         color: Colors.white24,
  155.         child: new Slidable(
  156.           key: new Key(item.name),
  157.           controller: slidableController,
  158.           direction: direction,
  159.  
  160.           slideToDismissDelegate: new SlideToDismissDrawerDelegate(
  161.  
  162.             onDismissed: (actionType) {
  163.               _showSnackBar(
  164.                   context,
  165.                   actionType == SlideActionType.primary
  166.                       ? 'Dismiss Archive'
  167.                       : 'Dimiss Delete');
  168.               setState(() {
  169.                 listItems.removeAt(index);
  170.               });
  171.             },
  172.           ),
  173.           delegate: _getDelegate(item.index),
  174.           actionExtentRatio: 0.30,
  175.           child: _buildRowItem(index),
  176.           secondaryActions: <Widget>[
  177.             new IconSlideAction(
  178.               caption: 'More',
  179.               color: Colors.grey.shade200,
  180.               icon: Icons.more_horiz,
  181.               onTap: () => _showSnackBar(context, 'More'),
  182.               closeOnTap: false,
  183.             ),
  184.             new IconSlideAction(
  185.               caption: 'Delete',
  186.               color: Colors.red,
  187.               icon: Icons.delete,
  188.               onTap: () => _showSnackBar(context, 'Delete'),
  189.             ),
  190.           ],
  191.         ),
  192.       ),
  193.     );
  194.   }
  195.  
  196.   void _showSnackBar(BuildContext context, String text) {
  197.     Scaffold.of(context).showSnackBar(SnackBar(content: new Text(text)));
  198.   }
  199.  
  200.   static SlidableDelegate _getDelegate(int index) {
  201.       switch (index % 4) {
  202.       case 0:
  203.         return new SlidableBehindDelegate();
  204.       case 1:
  205.         return new SlidableStrechDelegate();
  206.       case 2:
  207.         return new SlidableScrollDelegate();
  208.       case 3:
  209.         return new SlidableDrawerDelegate();
  210.       default:
  211.         return null;
  212.     }
  213.   }
  214. }
  215.  
  216. class _ListItem {
  217.   _ListItem({this.index, this.initials, this.name, this.subject, this.body});
  218.  
  219.   final int index;
  220.   final String initials;
  221.   final String name;
  222.   final String subject;
  223.   final String body;
  224. }
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement