popscope_ios 0.0.1 copy "popscope_ios: ^0.0.1" to clipboard
popscope_ios: ^0.0.1 copied to clipboard

PlatformiOS

A new Flutter Plugin for iOS.

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:popscope_ios/popscope_ios.dart';

/// 创建全局 Navigator Key
/// 
/// 这个 key 用于让插件能够访问 Flutter 的导航系统,
/// 从而在检测到左滑返回手势时自动调用 maybePop()
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

void main() {
  // 确保 Flutter 绑定已初始化
  WidgetsFlutterBinding.ensureInitialized();
  
  // 方法 1:设置 Navigator Key,启用自动导航处理
  // 当检测到左滑返回手势时,插件会自动调用 Navigator.maybePop()
  PopscopeIos.setNavigatorKey(navigatorKey);
  
  // 方法 2:设置左滑返回手势回调(可选)
  // 如果需要在返回时执行自定义逻辑,可以设置此回调
  // 注意:如果同时设置了 setNavigatorKey,会先自动返回,然后再执行此回调
  // PopscopeIos.setOnLeftBackGesture(() {
  //   print('检测到左滑返回手势');
  //   // 这里可以执行自定义逻辑
  // });
  
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  int _backGestureCount = 0;
  final _popscopeIosPlugin = PopscopeIos();

  @override
  void initState() {
    super.initState();
    initPlatformState();
    setupBackGestureListener();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion =
          await _popscopeIosPlugin.getPlatformVersion() ?? 'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  /// 设置系统返回手势监听
  /// 
  /// 这个方法演示了如何使用 setOnLeftBackGesture 来监听返回手势。
  /// 在这个示例中,我们同时设置了 setNavigatorKey 和 setOnLeftBackGesture,
  /// 所以执行顺序是:
  /// 1. 插件自动调用 Navigator.maybePop()
  /// 2. 执行这里设置的回调函数
  void setupBackGestureListener() {
    PopscopeIos.setOnLeftBackGesture(() {
      debugPrint('检测到系统返回手势!系统已自动调用 Navigator.maybePop()');
      
      // 更新计数(用于 UI 显示)
      if (mounted) {
        setState(() {
          _backGestureCount++;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // 重要:必须将 navigatorKey 关联到 MaterialApp
      // 这样插件才能访问导航系统
      navigatorKey: navigatorKey,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Popscope iOS Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text('Running on: $_platformVersion\n'),
              const SizedBox(height: 20),
              Text(
                '返回手势触发次数: $_backGestureCount',
                style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 40),
              ElevatedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => const SecondPage(),
                    ),
                  );
                },
                child: const Text('打开第二页'),
              ),
              const SizedBox(height: 20),
              const Text(
                '提示:在第二页尝试左滑返回\n系统会自动调用 Navigator.maybePop()',
                textAlign: TextAlign.center,
                style: TextStyle(color: Colors.grey),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class SecondPage extends StatelessWidget {
  const SecondPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('第二页'),
      ),
      body: const Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(Icons.swipe_left, size: 100, color: Colors.blue),
            SizedBox(height: 20),
            Text(
              '尝试左滑返回',
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 20),
            Text(
              '系统返回手势被拦截后\n会自动调用 Navigator.maybePop()',
              textAlign: TextAlign.center,
              style: TextStyle(color: Colors.grey),
            ),
          ],
        ),
      ),
    );
  }
}
0
likes
130
points
65
downloads

Publisher

unverified uploader

Weekly Downloads

A new Flutter Plugin for iOS.

Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on popscope_ios

Packages that implement popscope_ios