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

Flutter ListTile小部件:定义颜色时没有飞溅效果,flutter,dart,Flutter,Dart,在我的项目中,我有一个需要应用背景色的ListTile列表。 通过应用颜色,ListTiles将失去onTap飞溅效果 我尝试使用ListTiles的tileColor属性来实现背景色 我试图在容器中实现背景色,方法是在子容器中传递ListTile,但没有成功 //1(飞溅效果工作) ListTile(标题:文本(“没有颜色的ListTile”),onTap:(){}), //2(飞溅无效果工作) 列表砖( tileColor:Colors.blue, 标题:文本(“带颜色的ListTile

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

  • 我尝试使用ListTiles的tileColor属性来实现背景色
  • 我试图在容器中实现背景色,方法是在子容器中传递ListTile,但没有成功
//1(飞溅效果工作)
ListTile(标题:文本(“没有颜色的ListTile”),onTap:(){}),
//2(飞溅无效果工作)
列表砖(
tileColor:Colors.blue,
标题:文本(“带颜色的ListTile”),
onTap:({}),
//3(飞溅无效果工作)
容器(
颜色:颜色,蓝色,
孩子:ListTile(
标题:正文(
“有颜色的容器中无颜色的ListTile”),
onTap:({}))
完整示例:

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: () {}),
                ],
              )),
        ));
    }
}

//版权所有(c)2019,Dart项目作者。请参阅作者档案
//详情请参阅。版权所有。此源代码的使用受
//可以在许可证文件中找到的BSD样式的许可证。
进口“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
debugShowCheckedModeBanner:false,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:颜色。灰色[700],
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:容器(
宽度:400,
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
//1(飞溅效应工作)
ListTile(标题:文本(“没有颜色的ListTile”),onTap:(){}),
//2(飞溅无效果工作)
列表砖(
tileColor:Colors.blue,
标题:文本(“带颜色的ListTile”),
onTap:({}),
//3(飞溅无效果工作)
容器(
颜色:颜色,蓝色,
孩子:ListTile(
标题:正文(
“有颜色的容器中无颜色的ListTile”),
onTap:({}))
],
)),
));
}
}

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

  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                                                                                                                                             
   }
完整示例:

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: () {}),
                ],
              )),
        ));
    }
}

类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
小部件_buildTile({
小部件标题,
彩色tileColor,
色彩斑斓,
功能onTap,
}) {                                                                                                                                                         
退回资料(
颜色:tileColor,
孩子:英克韦尔(
splashColor:splashColor,
onTap:onTap,
儿童:ListTile(
标题:标题,,
),//ListTile
),//InkWell
);//材料
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:颜色。灰色[700],
appBar:appBar(
标题:文本(widget.title),
),
正文:分