flutter - 使用可拖动的颤振创建文件夹
问题描述
我正在使用颤振制作一个安卓启动器。我已经使用 DragAndDropGridView 制作了一个应用程序列表,但是如何才能创建图标文件夹?
解决方案
我做了一个示例应用程序作为演示,但我没有使用 DragAndDropGridView 包,因为它已经过时了。相反,我使用了 reorderables 包。
编码
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<Widget> apps = <Widget>[
AppIcon(),
AppIcon(),
AppIcon(),
AppIcon(),
AppFolder(),
AppIcon()
];
void _onReorder(int oldIndex, int newIndex) {
setState(() {
Widget row = apps.removeAt(oldIndex);
apps.insert(newIndex, row);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
ReorderableWrap(
spacing: 8.0,
runSpacing: 4.0,
padding: const EdgeInsets.all(8),
children: apps,
onReorder: _onReorder,
onNoReorder: (int index) {},
onReorderStarted: (int index) {}),
]),
);
}
}
class AppIcon extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
height: 100,
width: 100,
alignment: Alignment.center,
color: generateRandomColor(),
child: Text("App Icon"));
}
}
class AppFolder extends StatefulWidget {
@override
_AppFolderState createState() => _AppFolderState();
}
class _AppFolderState extends State<AppFolder> {
List<Widget> folderApps = <Widget>[
AppIcon(),
AppIcon(),
AppIcon(),
AppIcon(),
AppIcon(),
AppIcon(),
AppIcon(),
AppIcon(),
AppIcon(),
];
void _onReorder(int oldIndex, int newIndex) {
setState(() {
Widget row = folderApps.removeAt(oldIndex);
folderApps.insert(newIndex, row);
});
}
@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
backgroundColor: Colors.white,
content: Container(
alignment: Alignment.center,
height: 350,
width: 350,
child: ReorderableWrap(
spacing: 8.0,
runSpacing: 4.0,
padding: const EdgeInsets.all(8),
children: folderApps,
onReorder: _onReorder,
onNoReorder: (int index) {},
onReorderStarted: (int index) {}),
),
);
});
},
child: Image.network(
"https://icons.iconarchive.com/icons/alecive/flatwoken/512/Apps-Folder-icon.png",
height: 100,
width: 100));
}
}
Color generateRandomColor() {
Random random = Random();
return Color.fromARGB(
255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
[代码可能并不完美,仅用于演示]
我使用 TextButton 创建了一个应用程序文件夹,当它被按下时会显示一个 AlertDialog,在文件夹内还有另一个 ReorderableWrap 和更多应用程序。
视频
推荐阅读
- c# - 如何使用 REST API 更新 SharePoint 在线文件的元数据?
- c# - 自动将命名空间添加到 Unity C# 脚本
- javascript - 从 Javascript 对象中删除项目
- angular - Angular2:订阅对象以将对象的值与服务绑定
- python - 命名结构(例如 namedtuple)中的可变长度参数列表 (*args)
- c# - ASP.NET Core 3.0 的本地化不起作用
- c# - 我的“out IDbConnection 参数”需要什么值?
- ios - SwiftUI:Slider 在导航栏项目的前导/尾随时的奇怪行为
- python - 为什么从 cmd 运行 .py 与使用 anaconda 提示符运行时自动换行(QTableWidget、PYQT5)的绘制方式不同?
- html - css3中的元素定位