回调函数 应用
定义函数
getFiles(DirNode dir) {
print("getFiles");
files = [];
var list = FileProvider.listFiles(Directory(dir.path));
files.addall(list);
}
定义回调函数 的引用 方便传递函数
void Function(DirNode) onGetFiles;
在 点击事件中调用 回调函数
if (onGetFiles != null) {
print("object");
onGetFiles!(node);
}
},
关联回调函数和 函数体
Widget leftPanel(ExpandedTileController? controller){
var tile = RecursiveExpandedTile(_currentDir!, (DirNode dir) {
getFiles(dir);
}, hideHeadOnFirstCall: true, controller: controller);
return Container(
alignment: Alignment.topLeft,
width: _showLeftPanel ? width : 0,
child: tile,
);
}
class RecursiveExpandedTile extends StatelessWidget {
final DirNode node;
final bool hideHeadOnFirstCall;
ExpandedTileController? controller;
void Function(DirNode) onGetFiles;
RecursiveExpandedTile(
this.node,
this.onGetFiles, {
this.hideHeadOnFirstCall = false,
this.controller,
});
@override
Widget build(BuildContext context) {
final tileController =
controller ?? ExpandedTileController(isExpanded: false);
return ExpandedTile(
enabled: false,
hiddenHead: hideHeadOnFirstCall,
isExpanded: controller != null ? true : false,
onTap: () {
print("dirNode.name: ${node.name}");
if (onGetFiles != null) {
print("object");
onGetFiles!(node);
}
},
title: Text(node.name),
leading: node.children.isNotEmpty ? null : Container(),
content: Column(
children: node.children
?.map((child) => RecursiveExpandedTile(child, onGetFiles))
?.toList() ??
[],
),
controller: tileController,
);
}
}