[Day10] Flutter GetX flutter_slidable ListView滑动

Flutter slidable

Flutter listView左右滑动後出现的选项,(iOS的 tableView swipe actions)
RsDCnDgf2_sRUufuyqh_1w

这次将在首页的ListView改成slidable
在ListView的itemBuilder里建立 _slidableCell() function

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('FirstPage')),
      body: SafeArea(
        child: Container(
          color: Colors.grey[50],
          child: _buildListView(),
        ),
      ),
    );
  }

  Widget _buildListView() {
    return ListView.builder(
      physics: BouncingScrollPhysics(),
      shrinkWrap: true,
      itemCount: controller.dataList.length,
      itemBuilder: (BuildContext context, int index) {
        final title = controller.dataList[index];
        return _slidableCell(title);
      },
    );
  }

Card, ListTile是cell的本体(外层是Slidable)
secondaryActions 这个属性里面带入 List
这次范例显示两个侧滑选项

  Widget _slidableCell(String title) {
    return Slidable(
      actionPane: SlidableStrechActionPane(),
      secondaryActions: [
        Padding(
          padding: const EdgeInsets.all(3.0),
          child: SliderActionWidget(
            text: "选项1",
            backgroundColor: Colors.lightBlue[300],
            iconData: Icons.drive_file_move_outline,
            didSelected: () async {
              print("点击了 $title 选项1");
            },
          ),
        ),
        Padding(
          padding: const EdgeInsets.all(3.0),
          child: SliderActionWidget(
            text: "选项2",
            backgroundColor: Colors.orange[300],
            iconData: Icons.delete_forever,
            didSelected: () {
              print("点击了 $title 选项2");
            },
          ),
        ),
      ],
      child: Card(
        elevation: 3.0,
        child: ListTile(
          title: Text(title),
          trailing: Icon(Icons.arrow_forward_ios),
          onTap: () => Get.toNamed("/FirstPage/$title"),
        ),
      ),
    );
  }

里面的SliderActionWidget则是写一个widget class
construct的时候带入
text: 选单显示的字, backgroundColor: 选单背景颜色,
iconData: 带入Icon, didSelected: 选单被点击後的回传


///Cell左滑出现的小选单
class SliderActionWidget extends StatelessWidget {
  const SliderActionWidget(
      {Key? key,
      required this.text,
      required this.backgroundColor,
      required this.iconData,
      required this.didSelected})
      : super(key: key);
  final String text;
  final Color? backgroundColor;
  final IconData iconData;
  final VoidCallback didSelected;

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: const BorderRadius.all(Radius.circular(5)),
      child: IconSlideAction(
        caption: text,
        color: backgroundColor,
        icon: iconData,
        onTap: () => didSelected(),
      ),
    );
  }
}

最後效果如下

j2Rfd4w8jydI3EvO3g

本篇的GitHub source code

下一篇将为大家介绍 Flutter toast & Overlay


<<:  Day 10 学习线上服务思考用户的数位防身术-国内篇

>>:  铁人赛 Day24 -- JavaScript 初体验(二) -- 点击後换图片

Day4 参加职训(机器学习与资料分析工程师培训班),记录学习内容(6/30-8/20)

上午: 人工智慧AIoT资料分析应用系统框架设计与实作 初步介绍网站,WWW的历史以及各个浏览器的演...

[Python 爬虫这样学,一定是大拇指拉!] DAY03 - 关於 Python (2)

了解 Python 的特性之後,来看看 Python 还有什麽厉害之处吧~ 所以 Python,你有...

视觉化KBARS(3)-service、serviceImpl

今天要写service、serviceImpl两个部分, (1)第一部分,service新增oneM...

35.Local Storage

Local Storage (更准确地说是“Web Storage”)适合存储你希望进行持久化的较小...

[Day 11]在你顺利的时候来一拳才是标配(前端篇)

挑战目标: MockNative Camp 今天要来实作Nav Detail的部分, 昨天将相关的L...