简体   繁体   English

ListTile tileColor溢出ListView

[英]ListTile tileColor overflowing ListView

I have a ListView with ListTiles which set the tileColor property.我有一个带有 ListTiles 的 ListView,它设置了 tileColor 属性。 All of the text and icons in the ListTile is clipped while the tileColor is visible beyond the ListView. ListTile 中的所有文本和图标都被剪裁,而 tileColor 在 ListView 之外可见。 How can I clip the entire ListTile so that nothing is visible beyond the ListView?如何剪辑整个 ListTile,以便在 ListView 之外看不到任何东西? I have tried wrapping ListView in various containers and the ListTile in a Container with no luck.我尝试将 ListView 包装在各种容器中,并将 ListTile 包装在 Container 中,但没有成功。

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.teal,
      ),
      home: const MyHomePage(title: 'Reminders'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List _reminders = <String>[];

  void _addReminder(String text) {
    setState(() {
      _reminders.add(text);
    });
  }

  Widget _buildRow(String text, int i) {
    return ListTile(
      title: Text('[enter image description here][1]Reminder'),
      trailing: IconButton(
          onPressed: () {
            setState(() {
              _reminders.removeAt(i);
            });
          },
          icon: Icon(Icons.delete)),
      subtitle: Text(text),
      tileColor: Colors.lightBlue,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
    );
  }

  void _showMenu() {}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: [IconButton(onPressed: _showMenu, icon: const Icon(Icons.list))],
      ),
      body: Column(
        children: <Widget>[
          Container(
            height: 400,
            child: ListView.builder(
              padding: EdgeInsets.all(8),
              itemBuilder: (context, i) => _buildRow(_reminders[i], i),
              itemCount: _reminders.length,
            ),
          ),
          Container(
            height: 100,
            width: double.infinity,
            child: Text('hello'),
          )
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _addReminder('hello there');
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

图片

// Copyright (c) 2019, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.teal,
      ),
      home: const MyHomePage(title: 'Reminders'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // ignore: prefer_final_fields
  List _reminders = <String>[];

  void _addReminder(String text) {
    setState(() {
      _reminders.add(text);
    });
  }

  Widget _buildRow(String text, int i) {
    return ListTile(
      title: const Text('Reminder'),
      trailing: IconButton(
          onPressed: () {
            setState(() {
              _reminders.removeAt(i);
            });
          },
          icon: const Icon(Icons.delete)),
      subtitle: Text(text),
      tileColor: Colors.lightBlue,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
    );
  }

  void _showMenu() {}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: [IconButton(onPressed: _showMenu, icon: const Icon(Icons.list))],
      ),
      body: ListView.separated(
              padding: const EdgeInsets.all(8),
              itemBuilder: (context, i) => _buildRow(_reminders[i], i),
              itemCount: _reminders.length, separatorBuilder: (BuildContext context, int index) {
              return const SizedBox(height:10);},
            ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _addReminder('hello there');
        },
        child: const Icon(Icons.add),
      ),
    );
  }

}

截屏

Here is my code with solution这是我的代码和解决方案


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.teal,
      ),
      home: const MyHomePage(title: 'Reminders'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List _reminders = <String>[];

  void _addReminder(String text) {
    setState(() {
      _reminders.add(text);
    });
  }

  Widget _buildRow(String text, int i) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 5),
      child: ListTile(
        title: Text('[enter image description here][1]Reminder'),
        trailing: IconButton(
            onPressed: () {
              setState(() {
                _reminders.removeAt(i);
              });
            },
            icon: Icon(Icons.delete)),
        subtitle: Text(text),
        tileColor: Colors.lightBlue,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ),
    );
  }

  void _showMenu() {}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: [
          IconButton(onPressed: _showMenu, icon: const Icon(Icons.list))
        ],
      ),
      body: Column(
        children: <Widget>[
          SizedBox(
            height: 200,
            child: ListView.builder(
              scrollDirection: Axis.vertical,
              padding: EdgeInsets.all(8),
              itemBuilder: (context, i) => _buildRow(_reminders[i], i),
              itemCount: _reminders.length,
            ),
          ),
          Expanded(
            child: Container(
              color: Colors.white,
              width: double.infinity,
              child: Text('hello'),
            ),
          )
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _addReminder('hello there');
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

here is ss这是ss

在此处输入图像描述

To solve your problem, wrap the Tile in a Material.要解决您的问题,请将 Tile 包裹在 Material 中。 Here is a tested solution.这是一个经过测试的解决方案。 The explanation is below the code.解释在代码下方。

The short version:简短版本:

  Widget _buildRow(String text, int i) {
    return Material(
        child: ListTile(
          title: Text('[enter image description here][$i]Reminder'),
          trailing: IconButton(
            onPressed: () {
              setState(() {
                _reminders.removeAt(i);
              });
            },
            icon: const Icon(Icons.delete)),
        subtitle: Text(text),
        tileColor: Colors.lightBlue,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
    ));
  }

The complete, working example:完整的工作示例:

```
import 'package:flutter/material.dart';

  void main() {
   runApp(const MyApp());
  }

  class MyApp extends StatelessWidget {
    const MyApp({Key? key}) : super(key: key);

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.teal,
        ),
        home: const MyHomePage(title: 'Reminders'),
      );
    }
  }

  class MyHomePage extends StatefulWidget {
    const MyHomePage({Key? key, required this.title}) : super(key: key);

    final String title;

    @override
    State<MyHomePage> createState() => _MyHomePageState();
  }

  class _MyHomePageState extends State<MyHomePage> {
    final List _reminders = <String>[];

    void _addReminder(String text) {
      setState(() {
        _reminders.add(text);
      });
    }

    Widget _buildRow(String text, int i) {
      return Material(child: ListTile(
        title: Text('[enter image description here][$i]Reminder'),
        trailing: IconButton(
            onPressed: () {
              setState(() {
                _reminders.removeAt(i);
              });
            },
            icon: const Icon(Icons.delete)),
        subtitle: Text(text),
        tileColor: Colors.lightBlue,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
      ));
    }

    void _showMenu() {}

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
          actions: [IconButton(onPressed: _showMenu, icon: const Icon(Icons.list))],
        ),
        body: Column(
          children: <Widget>[
            Container(
              height: 400,
              child: ClipRect(
                child: ListView.builder(
                  shrinkWrap: true,
                  padding: const EdgeInsets.all(8),
                  itemBuilder: (context, i) =>              _buildRow(_reminders[i], i),
                  itemCount: _reminders.length,
                ),
              )
            ),
            Container(
              color: Colors.white,
              width: double.infinity,
              child: Text('hello'),
            )
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            _addReminder('hello there');
          },
          child: const Icon(Icons.add),
        ),
      );
    }
  }
```

应用程序运行代码示例

This might be considered a bug, but it is expected behavior.这可能被认为是一个错误,但这是预期的行为。 The tile colors are painted by the first Material widget ancestor of the ListTile (I believe this allows it to optimize out expensive clipping). tile colors 由 ListTile 的第一个 Material 小部件祖先绘制(我相信这可以优化昂贵的裁剪)。 See the ListTile documentation for more information.有关详细信息,请参阅ListTile 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM