0

我正在 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 中的错误还是有更简单的解决方案?

4

1 回答 1

0

简单的解决方案只需将卡边距设置为 0

换成这张卡

child: Card(color: Colors.blue, margin: EdgeInsets.all(0), child: 
ListTile(title: Text('$item'),)

如果您想在 Items 之间留一个空格,您可以添加 SizedBox 或将 Dismissible 包装在 Padding 上并设置一个值

于 2020-07-08T19:54:19.310 回答