Flutter滑动弹出管理插件slideable_pop_scope的使用

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

Flutter滑动弹出管理插件slideable_pop_scope的使用

slideable_pop_scope 是一个易于使用且适应 iOS 滑动手势返回功能的弹出管理插件。使用此插件可以解决在使用 Flutter 官方 PopScope 时,iOS 的滑动手势返回功能失效的问题。

使用说明

要使用此插件,首先需要在 pubspec.yaml 文件中添加依赖:

dependencies:
  slideable_pop_scope: ^x.y.z

然后运行 flutter pub get 来安装依赖。

示例代码

以下是一个完整的示例代码,展示了如何使用 SlideablePopScope 插件来管理页面之间的滑动返回手势。

导入库

在你的 Dart 文件中导入 slideable_pop_scope 库:

// main.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:slideable_pop_scope/slideable_pop_scope.dart';

使用 SlideablePopScope

在需要处理滑动返回的手势的页面中使用 SlideablePopScope,并实现 onWillPop 方法来处理返回逻辑:

// my_screen.dart
[@override](/user/override)
Widget build(BuildContext context) {
  return SlideablePopScope(
    child: _MyScreenContent(),
    onWillPop: _onWillPop,
  );
}

Future<bool> _onWillPop() async {
  // 显示确认对话框,询问用户是否确定返回
  bool shouldPop = await showDialog<bool>(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: const Text('确定要返回吗?'),
        actions: <Widget>[
          TextButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: const Text('确定'),
          ),
        ],
      );
    },
  ) ?? false;

  return shouldPop;
}

完整示例

以下是一个完整的示例代码,展示了如何在一个应用中使用 SlideablePopScope

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:slideable_pop_scope/slideable_pop_scope.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  
  [@override](/user/override)
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'SlideablePopScope Example',
      home: PageA(),
    );
  }
}

class PageA extends StatelessWidget {
  const PageA({super.key});
  
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("PageA")),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            Navigator.push(context, CupertinoPageRoute(
              builder: (context) {
                return const PageB();
              },
            ));
          },
          child: const Text("到 PageB"),
        ),
      ),
    );
  }
}

class PageB extends StatefulWidget {
  const PageB({super.key});
  
  [@override](/user/override)
  State<PageB> createState() => _PageBState();
}

class _PageBState extends State<PageB> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return SlideablePopScope(
      onWillPop: () async {
        return await _showPopConfirmDialog();
      },
      child: Scaffold(
        appBar: AppBar(title: const Text("PageB")),
        body: const Center(
          child: Text("PageB"),
        ),
      ),
    );
  }

  Future<bool> _showPopConfirmDialog() async {
    bool? res = await showDialog<bool>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: const Text("返回 PageA?"),
          actions: [
            ElevatedButton(
              onPressed: () {
                Navigator.pop(context, false);
              },
              child: const Text("取消"),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.pop(context, true);
              },
              child: const Text("确定"),
            ),
          ],
        );
      },
    );
    return res ?? false;
  }
}

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

1 回复

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


当然,下面是一个关于如何在Flutter项目中使用slideable_pop_scope插件的示例代码。slideable_pop_scope是一个增强版的PopScope,它允许你通过滑动操作来触发弹出(如返回上一级页面或关闭对话框)。

首先,确保你已经在pubspec.yaml文件中添加了slideable_pop_scope依赖:

dependencies:
  flutter:
    sdk: flutter
  slideable_pop_scope: ^latest_version  # 请替换为最新版本号

然后,运行flutter pub get来获取依赖。

接下来,你可以在你的Flutter应用中使用SlideablePopScope。以下是一个简单的示例,展示如何在页面中使用它:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SlideablePopScopeDemo(),
    );
  }
}

class SlideablePopScopeDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SlideablePopScope Demo'),
      ),
      body: SlideablePopScope(
        onWillPop: () async {
          // 这里可以添加一些自定义逻辑,比如显示确认对话框
          return showDialog(
            context: context,
            builder: (context) => AlertDialog(
              title: Text('确定退出吗?'),
              content: Text('你将离开当前页面。'),
              actions: <Widget>[
                TextButton(
                  onPressed: () => Navigator.pop(context, false),
                  child: Text('取消'),
                ),
                TextButton(
                  onPressed: () => Navigator.pop(context, true),
                  child: Text('确定'),
                ),
              ],
            ),
          ) == true;
        },
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                '滑动屏幕边缘返回',
                style: TextStyle(fontSize: 24),
              ),
              SizedBox(height: 20),
              ElevatedButton(
                onPressed: () {
                  // 打开一个新页面以测试滑动返回功能
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => SecondPage()),
                  );
                },
                child: Text('打开新页面'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('第二页'),
      ),
      body: Center(
        child: Text('这是第二页'),
      ),
    );
  }
}

在这个示例中,SlideablePopScope被用来包裹整个Scaffold。当用户尝试通过滑动屏幕边缘返回时,onWillPop回调会被触发,这里我们显示了一个确认对话框。如果用户点击“确定”,则返回true,页面将会关闭;否则返回false,页面保持打开状态。

注意,slideable_pop_scope的具体使用可能依赖于其最新版本,因此请参考其官方文档和示例代码以获取最新的使用方法和最佳实践。

回到顶部