Android 当抽屉里有不可接受的东西时,如何让它关闭?
我有一个结尾抽屉,里面有一个列表视图,里面有一些可驳回的东西 在每个dismissible中,我都设置了“direction:DismissDirection.endToStart” 这将正确地停止抽屉关闭方向上的泄气,但不会关闭抽屉 如何允许抽屉继续关闭,同时仍能以另一种方式关闭?尝试以下操作:Android 当抽屉里有不可接受的东西时,如何让它关闭?,android,flutter,dart,flutter-layout,Android,Flutter,Dart,Flutter Layout,我有一个结尾抽屉,里面有一个列表视图,里面有一些可驳回的东西 在每个dismissible中,我都设置了“direction:DismissDirection.endToStart” 这将正确地停止抽屉关闭方向上的泄气,但不会关闭抽屉 如何允许抽屉继续关闭,同时仍能以另一种方式关闭?尝试以下操作: Navigator.of(context.pop() 我通过使用IgnorePointer和Listener作为您的Dismissible的家长,设法解决了您的问题 当用户打开抽屉时,他会发现默认情况
Navigator.of(context.pop() 我通过使用
IgnorePointer
和Listener
作为您的Dismissible
的家长,设法解决了您的问题
当用户打开抽屉时,他会发现默认情况下禁用了所有可解除功能
,但在抽屉关闭的相反方向上进行一次滑动就会激活它们。这里的颜色从灰色变为黑色
要再次关闭抽屉
,您必须停用Dismissibles
,这是通过一次小的滑动来完成的,然后您可以通过结束开始滑动来轻松关闭
我知道您希望在不将手指从屏幕上提起的情况下实现类似的行为,但我无法做到这一点,因为更改状态需要完成指针事件
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
final appTitle = 'Dismissibles demonstration';
MyApp({Key key,}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: widget.appTitle,
home: MyHomePage(title: widget.appTitle),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState() ;
}
class _MyHomePageState extends State<MyHomePage> {
final items = List<String>.generate(100, (i) => "Item ${i + 1}");
var ignoreSwitch = true ;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Center(child: Text('Example')),
drawer: Drawer(
child: Listener(
onPointerMove: (PointerMoveEvent event) {
print(event.delta.dx);
if(event.delta.dx > 0.0){
print('all items enabled');
setState(() {
ignoreSwitch = false ;
});
}
},
child: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
if(index == 0){
return DrawerHeader(child: Text('Drawer Header',style: TextStyle(fontSize: 36.0),),decoration: BoxDecoration(color: Colors.cyan,),);
}
return Listener(
onPointerMove: (PointerMoveEvent event) {
if(event.delta.dx > 0.0){
setState(() {
print('Inner listener working ! item $index enabled');
ignoreSwitch = false ;
});
}else{
setState(() {
print('Inner listener working ! item $index disabled');
ignoreSwitch = true ;
});
}
},
child: IgnorePointer(
ignoring: ignoreSwitch,
child: Dismissible(
key: Key(item),
direction: DismissDirection.startToEnd,
background: Container(
color: Colors.green,
),
onDismissed: (direction) {
// Remove the item from our data source.
setState(() {
items.removeAt(index);
});
},
child: ListTileTheme(
textColor: ignoreSwitch ? Colors.grey : Colors.black,
style: ListTileStyle.drawer,
child: ListTile(
title: Text('item $index', style: TextStyle(fontSize: 18.0)),
onTap: () {
//Navigator.pop(context);
},
),
)
),
),
);
},
),
)
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了StatefulWidget{
最终appTitle=‘驳回证明’;
MyApp({Key,}):超级(Key:Key);
@凌驾
_MyAppState createState()=>\u MyAppState();
}
类MyAppState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:widget.appTitle,
主页:MyHomePage(标题:widget.appTitle),
);
}
}
类MyHomePage扩展StatefulWidget{
最后的字符串标题;
MyHomePage({Key,this.title}):超级(Key:Key);
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
final items=List.generate(100,(i)=>“Item${i+1}”);
var ignoreSwitch=true;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:Text(widget.title)),
正文:居中(子项:文本(“示例”),
抽屉(
孩子:听众(
onPointerMove:(PointerMoveEvent事件){
打印(event.delta.dx);
如果(event.delta.dx>0.0){
打印(“所有项目已启用”);
设置状态(){
ignoreSwitch=false;
});
}
},
子项:ListView.builder(
itemCount:items.length,
itemBuilder:(上下文,索引){
最终项目=项目[索引];
如果(索引==0){
返回抽屉阅读器(子项:文本('DrawerHeader',样式:TextStyle(fontSize:36.0),),装饰:框装饰(颜色:Colors.cyan,),);
}
返回侦听器(
onPointerMove:(PointerMoveEvent事件){
如果(event.delta.dx>0.0){
设置状态(){
打印('internallistener working!item$index enabled');
ignoreSwitch=false;
});
}否则{
设置状态(){
打印('internallistener working!item$index disabled');
ignoreSwitch=true;
});
}
},
孩子:无知的指针(
忽略:忽略开关,
孩子:可鄙(
键:键(项),
方向:DismissDirection.startToEnd,
背景:集装箱(
颜色:颜色。绿色,
),
onDismissed:(方向){
//从数据源中删除该项。
设置状态(){
项目移除(索引);
});
},
孩子:我是李斯特(
text颜色:忽略开关?颜色。灰色:颜色。黑色,
样式:ListTileStyle.drawer,
孩子:ListTile(
标题:文本('item$index',样式:TextStyle(fontSize:18.0)),
onTap:(){
//Navigator.pop(上下文);
},
),
)
),
),
);
},
),
)
),
);
}
}
这是一种变通方法,但可以防止抽屉默认的平滑动画(例如,可以拖动并仅在一半的时候关闭动画)。为此,忽略指针和侦听器似乎非常方便。我将与他们合作,看看我是否能更接近预期的设计。