adaptive_pop_scope 1.0.5 copy "adaptive_pop_scope: ^1.0.5" to clipboard
adaptive_pop_scope: ^1.0.5 copied to clipboard

Custom pop scope widget to allow Apple user (iOS, iPadOS and macOS) uses the back gesture


Buy Me Coffee Badge Linkedin Badge Instagram Badge Pub Dev Badge

Adaptive pop scope a custom pop scope widget to allow apple user backs with apple back gesture

Preview #

iOS Android

Getting started #

Add the adaptive_pop_scope lib to your pubspec.yaml then run the flutter pub get

dependencies:
  adaptive_pop_scope:

import the lib by adding this line code

import 'package:adaptive_pop_scope/widget.dart';

Usage #

Adaptive Will Pop Scope adds two optional params swipeWidth to determines the maximum width that user can swipe (by default it was assigned to the screen width) and swipeThreshold to indicates the onWillPop will be called if the user swipe over this value (by default it was assigned to one third of the swipeWidth).

import 'package:adaptive_pop_scope/couple.dart';
import 'package:adaptive_pop_scope/widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

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

  static MaterialPageRoute get route => MaterialPageRoute(
        builder: (_) => const FourthPage(),
      );

  @override
  State<FourthPage> createState() => _FourthPageState();
}

class _FourthPageState extends State<FourthPage> {
  late final _nameInput = TextEditingController();

  @override
  void dispose() {
    _nameInput.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final screenWidth = MediaQuery.of(context).size.width;
    return AdaptivePopScope(
      onWillPop: () async {
        final shouldPop = await _showAlertDialog(context);
        final name = _nameInput.text.trim();
        return Couple(shouldPop ?? false, name.isEmpty ? null : name);
      },
      swipeWidth: screenWidth,
      swipeThreshold: screenWidth / 2,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Fourth Page'),
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        ),
        body: Container(
          alignment: Alignment.center,
          margin: const EdgeInsets.all(16),
          child: TextFormField(
            decoration: const InputDecoration(labelText: 'Enter your name'),
            controller: _nameInput,
          ),
        ),
      ),
    );
  }

  Future<bool?> _showAlertDialog(BuildContext context) {
    final name = _nameInput.text.trim();
    if (name.isEmpty) return Future.value(true);
    return showCupertinoModalPopup<bool>(
      context: context,
      builder: (_) => CupertinoAlertDialog(
        title: const Text('Are you sure'),
        content: const Text('you want to navigate away from this page?'),
        actions: <CupertinoDialogAction>[
          CupertinoDialogAction(
            isDefaultAction: true,
            onPressed: () => Navigator.pop(context),
            child: const Text('No'),
          ),
          CupertinoDialogAction(
            isDestructiveAction: true,
            onPressed: () => Navigator.pop(context, true),
            child: const Text('Yes'),
          ),
        ],
      ),
    );
  }
}
9
likes
160
points
395
downloads

Publisher

unverified uploader

Weekly Downloads

Custom pop scope widget to allow Apple user (iOS, iPadOS and macOS) uses the back gesture

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on adaptive_pop_scope