我在Flutter中构建了一个简单的listView,其中的“单元格”是带有固定边距的简单卡片。当删除这些卡片时,“边距”会覆盖可删除的背景,从而导致难看的设计。我创建了一个示例应用程序来演示这个问题:
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
// MyApp is a StatefulWidget. This allows updating the state of the
// widget when an item is removed.
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
@override
MyAppState createState() {
return MyAppState();
}
}
class MyAppState extends State<MyApp> {
final items = List<String>.generate(20, (i) => "Item ${i + 1}");
@override
Widget build(BuildContext context) {
final title = 'Dismissing Items';
return MaterialApp(
title: title,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text(title),
),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return Dismissible(
// Each Dismissible must contain a Key. Keys allow Flutter to
// uniquely identify widgets.
key: Key(item),
// Provide a function that tells the app
// what to do after an item has been swiped away.
onDismissed: (direction) {
// Remove the item from the data source.
setState(() {
items.removeAt(index);
});
// Then show a snackbar.
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text("$item dismissed")));
},
// Show a red background as the item is swiped away.
background: Container(color: Colors.red),
child: Card(color: Colors.blue, margin: EdgeInsets.all(9), child: ListTile(title: Text('$item'))),
);
},
),
),
);
}
}
这会在消除时产生以下设计:
也不可能把可撤销的卡放进卡里,因为你不会把卡刷走。这是Flutter的一个bug还是有更简单的解决方案?
3条答案
按热度按时间to94eoyn1#
一个简单的解决方案只是设置卡边距为0
替换到此卡
如果你想在项目之间留一个空格,你可以添加SizedBox或者把Dismissible放在一个Padding上并设置一个值
pbwdgjma2#
在
Dismissible
的background: Container(...)
小部件中,可以添加与Card小部件相同的边距。例如
background: Container(color: Colors.red, margin: EdgeInsets.all(9))
gcuhipw93#
首先尝试用另一个小部件 Package Dismissible,以便保留它们周围的空间,例如使用填充
然后我们实现Dismissible小部件的内部部分,添加一个在滑动元素时呈现的容器,并且不要忘记呈现要滑动的元素本身
希望这有帮助!)