flutter_slidable icon indicating copy to clipboard operation
flutter_slidable copied to clipboard

How to close slidable on tap of anywhere on the screen outside Slidable widget

Open jaydeepkaria opened this issue 2 years ago • 9 comments

I want to close slidable even if click bottom tab or something on spp bar

jaydeepkaria avatar Sep 10 '23 06:09 jaydeepkaria

I am also facing same problem.

sharma-shubham12 avatar Oct 04 '23 10:10 sharma-shubham12

me too

AymericLeFeyer avatar Jan 31 '24 16:01 AymericLeFeyer

Same issue occurred, if anyone resolved please comment ans.

AniketDhanakFintaar avatar Feb 23 '24 12:02 AniketDhanakFintaar

flutter_swipe_action_cell, i have you this library

sharma-shubham12 avatar Feb 23 '24 16:02 sharma-shubham12

this code work for me, you guys will like it:

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

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

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

@override Widget build(BuildContext context) { return MaterialApp( home: SlidableOwner( child: Scaffold( appBar: AppBar( elevation: 0, title: const Text('Slidable from outside'), ), body: SlidableAutoCloseBehavior( closeWhenOpened: true, closeWhenTapped: true, child: ListView.builder( itemCount: 10, itemBuilder: (context, index) { return MyTile(index: index); }, ), ), ), ), ); } }

class MyText extends StatelessWidget { final int index;

const MyText({ super.key, required this.index, });

@override Widget build(BuildContext context) { return Slidable( closeOnScroll: true, endActionPane: const ActionPane( dragDismissible: false, motion: ScrollMotion(), children: [ SlidableAction( backgroundColor: Color(0xFFe0e0e0), icon: Icons.remove_circle_outline_outlined, autoClose: true, onPressed: null, ) ], ), child: SlidableOwnerTarget( id: index, child: Container( padding: const EdgeInsets.all(24), child: Text("HuyKaiser"), ), ), ); } }

class SlidableOwnerScope extends InheritedWidget { final SlidableOwnerState state;

const SlidableOwnerScope({ super.key, required super.child, required this.state, });

@override bool updateShouldNotify(SlidableOwnerScope oldWidget) { return false; } }

class SlidableOwner extends StatefulWidget { final Widget child;

const SlidableOwner({ super.key, required this.child, });

@override State<SlidableOwner> createState() => SlidableOwnerState();

static SlidableOwnerState of(BuildContext context) { return context.getInheritedWidgetOfExactType<SlidableOwnerScope>()!.state; } }

class SlidableOwnerState extends State<SlidableOwner> { final _controllers = <Object, SlidableController>{};

@override Widget build(BuildContext context) { return SlidableOwnerScope( state: this, child: widget.child, ); }

Future close(Object id) async { final controller = _controllers[id];

if (controller == null) return;

return controller.close();

}

Future closeAll() async => await Future.wait(_controllers.values.map((e) => e.close()).toList()); }

class SlidableOwnerTarget extends StatefulWidget { final Widget child; final Object id;

const SlidableOwnerTarget({ super.key, required this.child, required this.id, });

@override State<SlidableOwnerTarget> createState() => _SlidableOwnerTargetState(); }

class _SlidableOwnerTargetState extends State<SlidableOwnerTarget> { late SlidableOwnerState _slidableOwnerState;

@override void didChangeDependencies() { super.didChangeDependencies();

_slidableOwnerState = SlidableOwner.of(context);

_slidableOwnerState._controllers[widget.id] = Slidable.of(context)!;

}

@override void dispose() { super.dispose(); _slidableOwnerState._controllers.remove(widget.id); }

@override Widget build(BuildContext context) { return widget.child; } }

huykaiser avatar May 07 '24 14:05 huykaiser

still relevant

8thgencore avatar May 28 '24 17:05 8thgencore