ListTile 小部件:定义颜色时没有飞溅效果

chr*_*ico 5 dart flutter

在我的项目中,我有一个需要应用背景颜色的 ListTile 列表。通过应用颜色,ListTiles 会失去 onTap 飞溅效果。

  • 我尝试使用ListTiles的tileColor属性来实现背景颜色
  • 我尝试通过在子项中传递 ListTile 来实现容器中的背景颜色,但没有成功
// 1 (splash effect working)
ListTile(title: Text("ListTile without color"), onTap: () {}),


// 2 (splash no effect working)
ListTile(
         tileColor: Colors.blue,
         title: Text("ListTile with color"),
         onTap: () {}),


// 3 (splash no effect working)
Container(
         color: Colors.blue,
         child: ListTile(
         title: Text(
                    "ListTile without color in container with color"),
                     onTap: () {}))
Run Code Online (Sandbox Code Playgroud)

完整示例:

// 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(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[700],
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Container(
              width: 400,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  // 1 (splash effect working)
                  ListTile(title: Text("ListTile without color"), onTap: () {}),
                  // 2 (splash no effect working)
                  ListTile(
                      tileColor: Colors.blue,
                      title: Text("ListTile with color"),
                      onTap: () {}),
                  // 3 (splash no effect working)
                  Container(
                      color: Colors.blue,
                      child: ListTile(
                          title: Text(
                              "ListTile without color in container with color"),
                          onTap: () {}))
                ],
              )),
        ));
  }
}

Run Code Online (Sandbox Code Playgroud)

Ste*_*lli 7

一种解决方案是使用MaterialInkWell类,如下所示:

  Widget _buildTile({                                                                                                                                          
     Widget title,                                                                                                                                              
     Color tileColor,                                                                                                                                           
     Color splashColor,                                                                                                                                         
     Function onTap,                                                                                                                                            
   }) {                                                                                                                                                         
     return Material(                                                                                                                                           
       color: tileColor,                                                                                                                                        
       child: InkWell(                                                                                                                                          
         splashColor: splashColor,                                                                                                                              
         onTap: onTap,                                                                                                                                          
         child: ListTile(                                                                                                                                       
           title: title,                                                                                                                                  
         ), // ListTile                                                                                                                                         
       ), // InkWell                                                                                                                                            
     ); // Material                                                                                                                                             
   }
Run Code Online (Sandbox Code Playgroud)

完整示例:

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  Widget _buildTile({                                                                                                                                          
     Widget title,                                                                                                                                              
     Color tileColor,                                                                                                                                           
     Color splashColor,                                                                                                                                         
     Function onTap,                                                                                                                                            
   }) {                                                                                                                                                         
     return Material(                                                                                                                                           
       color: tileColor,                                                                                                                                        
       child: InkWell(                                                                                                                                          
         splashColor: splashColor,                                                                                                                              
         onTap: onTap,                                                                                                                                          
         child: ListTile(                                                                                                                                       
           title: title,                                                                                                                                  
         ), // ListTile                                                                                                                                         
       ), // InkWell                                                                                                                                            
     ); // Material                                                                                                                                             
   }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.grey[700],
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Container(
              width: 400,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  _buildTile(
                      tileColor: Colors.blue,
                      splashColor: Colors.black,
                      title: Text("ListTile with color"),
                      onTap: () {}),
                ],
              )),
        ));
    }
}

Run Code Online (Sandbox Code Playgroud)