Flutter滑动操作插件slider_action的使用

发布于 1周前 作者 zlyuanteng 来自 Flutter

Flutter滑动操作插件slider_action的使用

标题

Flutter Slider Action

内容

一个简单的的滑动解锁组件,灵感来自Android的Slide To Act。

示例代码

import 'package:flutter/material.dart';
import 'package:slider_action/slider_action.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Slide to act Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ListView(
          children: [
            Builder(
              builder: (context) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(),
                );
              },
            ),
            Builder(
              builder: (context) {
                final GlobalKey<SlideActionState> _key = GlobalKey();
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    key: _key,
                    onSubmit: () {
                      Future.delayed(Duration(seconds: 11), () =&gt; _key.currentState?.reset());
                    },
                  ),
                );
              },
            ),
            Builder(
              builder: (context) {
                final GlobalKey<SlideActionState> _key = GlobalKey();
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    key: _key,
                    onSubmit: () {
                      Future.delayed(Duration(seconds: 11), () =&gt; _key.currentState?.reset());
                    },
                    innerColor: Colors.black,
                    outerColor: Colors.white,
                  ),
                );
              },
            ),
            Builder(
              builder: (context) {
                final GlobalKey<SlideActionState> _key = GlobalKey();
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    key: _key,
                    onSubmit: () {
                      Future.delayed(Duration(seconds: 11), () =&gt; _key.currentState?.reset());
                    },
                    alignment: Alignment.centerRight,
                    child: Text(
                      'Unlock',
                      style: TextStyle(color: Colors.white),
                    ),
                    sliderButtonIcon: Icon(Icons.lock),
                  ),
                );
              },
            ),
            Builder(
              builder: (context) {
                final GlobalKey<SlideActionState> _key = GlobalKey();
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    key: _key,
                    onSubmit: () {
                      Future.delayed(Duration(seconds: 11), () =&gt; _key.currentState?.reset());
                    },
                    height: 100,
                  ),
                );
              },
            ),
            Builder(
              builder: (context) {
                final GlobalKey<SlideActionState> _key = GlobalKey();
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    key: _key,
                    onSubmit: () {
                      Future.delayed(Duration(seconds: 11), () =&gt; _key.currentState?.reset());
                    },
                    sliderButtonIconSize: 48,
                    sliderButtonYOffset: -20,
                  ),
                );
              },
            ),
            Builder(
              builder: (context) {
                final GlobalKey<SlideActionState> _key = GlobalKey();
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    key: _key,
                    onSubmit: () {
                      Future.delayed(Duration(seconds: 11), () =&gt; _key.currentState?.reset());
                    },
                    elevation: 24,
                  ),
                );
              },
            ),
            Builder(
              builder: (context) {
                final GlobalKey<SlideActionState> _key = GlobalKey();
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    key: _key,
                    onSubmit: () {
                      Future.delayed(Duration(seconds: 11), () =&gt; _key.currentState?.reset());
                    },
                    borderRadius: 16,
                    animationDuration: Duration(seconds: 11),
                  ),
                );
              },
            ),
            Builder(
              builder: (context) {
                final GlobalKey<SlideActionState> _key = GlobalKey();
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    key: _key,
                    onSubmit: () {
                      Future.delayed(Duration(seconds: 11), () =&gt; _key.currentState?.reset());
                    },
                    reversed: true,
                  ),
                );
              },
            ),
            Builder(
              builder: (context) {
                final GlobalKey<SlideActionState> _key = GlobalKey();
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    key: _key,
                    onSubmit: () {
                      Future.delayed(Duration(seconds: 11), () =&gt; _key.currentState?.reset());
                    },
                    submittedIcon: Icon(
                      Icons.done_all,
                      color: Colors.white,
                    ),
                  ),
                );
              },
            ),
            Builder(
              builder: (context) {
                final GlobalKey<SlideActionState> _key = GlobalKey();
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    key: _key,
                    onSubmit: () {
                      Future.delayed(Duration(seconds: 11), () =&gt; _key.currentState?.reset());
                    },
                  ),
                );
              },
            ),
            Builder(
              builder: (context) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SlideAction(
                    sliderRotate: false,
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter滑动操作插件slider_action的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter滑动操作插件slider_action的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,下面是一个关于如何在Flutter项目中使用slider_action插件的示例代码。slider_action插件允许你在滑动操作(如滑动删除)时执行特定的动作。不过需要注意的是,slider_action并不是一个广泛认知的官方或流行的Flutter插件,我假设你指的是一个类似功能的自定义或第三方插件。如果实际插件名称有所不同,请根据实际情况调整。

以下是一个假设的slider_action插件使用示例,这里我们用Flutter的Dismissible小部件来实现一个类似的滑动删除功能,因为Flutter原生支持这种操作。如果你有一个具体的slider_action插件,其使用方式可能会有所不同,但原理是相似的。

示例代码

首先,确保你的pubspec.yaml文件中添加了必要的依赖(这里假设没有特定的slider_action插件,而是使用Flutter内置的Dismissible):

dependencies:
  flutter:
    sdk: flutter

然后,创建一个简单的Flutter应用,展示如何使用Dismissible来实现滑动删除功能:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final List<String> items = List<String>.generate(20, (i) => "Item $i");

  void _removeItem(int index) {
    setState(() {
      items.removeAt(index);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Slidable List Demo'),
      ),
      body: ListView.builder(
        itemCount: items.length,
        itemBuilder: (context, index) {
          return Dismissible(
            key: Key(items[index]),
            direction: DismissDirection.horizontal,
            onDismissed: (direction) {
              // Remove the item from the data source.
              _removeItem(index);

              // Optionally show a snackbar.
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(
                  content: Text("${items[index]} dismissed"),
                  action: SnackBarAction(
                    label: 'UNDO',
                    onPressed: () {
                      // Reinsert the item at its original position.
                      setState(() {
                        items.insert(index, items[index]);
                      });
                    },
                  ),
                ),
              );
            },
            background: Container(color: Colors.red),
            secondaryBackground: Container(color: Colors.green),
            child: ListTile(
              title: Text('${items[index]}'),
            ),
          );
        },
      ),
    );
  }
}

解释

  1. 依赖:我们使用Flutter SDK中的flutter包。
  2. 主应用MyApp是应用的根小部件。
  3. 首页MyHomePage是一个包含列表的状态ful小部件。
  4. 列表数据items列表包含要显示的项。
  5. 删除项_removeItem方法从列表中移除项。
  6. 构建列表ListView.builder用于构建列表项。
  7. 可滑动项Dismissible小部件允许用户通过滑动来删除项。
  8. 背景色:滑动时显示红色和绿色的背景,分别对应左右滑动方向。
  9. SnackBar:在项被删除后显示一个SnackBar,并提供一个UNDO选项。

如果你有一个特定的slider_action插件,并且它的用法与上述示例有所不同,请查阅该插件的官方文档或源代码以获取准确的使用指南。如果插件提供了示例代码或文档,通常那是最好的起点。

回到顶部