gpt4 book ai didi

android - flutter :可通过卡和 margin 关闭

转载 作者:行者123 更新时间:2023-12-03 08:40:57 30 4
gpt4 key购买 nike

我正在 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'))),
);
},
),
),
);
}
}

这会在关闭时产生以下设计: Image of a Card covering up the background of the dismissible widget

也不可能将可驳回的内容放入卡中,因为那时您不会将卡刷走。这是 Flutter 中的错误还是有更简单的解决方案?

最佳答案

首先尝试用另一个小部件包裹 Dismissible,以便保留它们周围的空间,例如使用 Padding

  @override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(
left: 16,
right: 16,
),
child: Dismissible(...),
);
}
}

然后我们实现 Dismissible 小部件的内部部分,添加一个在我们滑动元素时将呈现的容器,并且不要忘记呈现我们将滑动的元素本身

  @override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.only(
left: 16,
right: 16,
),
child: Dismissible(
key: ValueKey(id),
background: Container(

// most importantly, do not forget to give the inner container a
// padding to the right so that our icon does not stick to the
// wall of the container when swiping

padding: const EdgeInsets.only(
right: 16,
),
color: Theme.of(context).colorScheme.error,
alignment: Alignment.centerRight,
child: const Icon(
Icons.delete,
color: Colors.white,
),
),
child: Card(

// also if you use a card as an element that will swipe then
// you need to remove its default space

margin: const EdgeInsets.all(0),
child: ListTile(...),
),
);
}
}

希望这有帮助!)

关于android - flutter :可通过卡和 margin 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62801515/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com