Change background color of ListTile upon selection in Flutter

前端 未结 12 1737
闹比i
闹比i 2020-12-15 03:10

I\'ve made a ListView in Flutter, but now I have some ListTiles in this ListView that can be selected. Upon selection, I want the back

相关标签:
12条回答
  • 2020-12-15 03:55

    I was able to change the background color of the ListTile using a BoxDecoration inside Container:

    ListView (
        children: <Widget>[
            new Container (
                decoration: new BoxDecoration (
                    color: Colors.red
                ),
                child: new ListTile (
                    leading: const Icon(Icons.euro_symbol),
                    title: Text('250,00')
                )
            )
        ]
    )
    
    0 讨论(0)
  • 2020-12-15 03:56

    I know that the original question has been answered, but I wanted to add how to set the color of ListTile while the tile is being pressed. The property you are looking for is called highlight color and it can be set by wrapping the ListTile in a Theme widget, like this:

    Theme(
      data: ThemeData(
        highlightColor: Colors.red,
      ),
      child: ListTile(...),
      )
    );
    
    

    Note: if the Theme widget resets the font of text elements inside the ListTile, just set its fontFamily property to the same value You used in other places in your app.

    0 讨论(0)
  • 2020-12-15 03:56

    An easy way would be to store the initial index in a variable and then change the state of that variable whenever tapped.

       ListView.builder(
                  shrinkWrap: true,
                  itemCount: 4,
                  itemBuilder: (context, index) {
                    return Container( //I have used container for this example. [not mandatory]
                        color: tappedIndex == index ? Colors.blue : Colors.grey,
                        child: ListTile(
                            title: Center(
                          child: Text('${index + 1}'),
                        ),onTap:(){
                              setState((){
                                tappedIndex=index;
                              });
                            }));
                  })
    

    full code:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: MyWidget(),
        );
      }
    }
    
    class MyWidget extends StatefulWidget {
      @override
      MyWidgetState createState() => MyWidgetState();
    }
    
    class MyWidgetState extends State<MyWidget> {
      int tappedIndex;
    
      @override
      void initState() {
        super.initState();
        tappedIndex = 0;
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
              ListView.builder(
                  shrinkWrap: true,
                  itemCount: 4,
                  itemBuilder: (context, index) {
                    return Container(
                        color: tappedIndex == index ? Colors.blue : Colors.grey,
                        child: ListTile(
                            title: Center(
                          child: Text('${index + 1}'),
                        ),onTap:(){
                              setState((){
                                tappedIndex=index;
                              });
                            }));
                  })
            ]));
      }
    }
    
    

    Dartpad link: https://dartpad.dev/250ff453b97cc79225e8a9c657dffc8a

    0 讨论(0)
  • 2020-12-15 03:58

    I have used as

    ListTile(
                    title: Text('Receipts'),
                    leading: Icon(Icons.point_of_sale),
                    tileColor: Colors.blue,
                  ),  
    
    0 讨论(0)
  • 2020-12-15 04:02

    Your answer has been answered in Github.

    Card(
      color: Colors.white,
      shape: ContinuousRectangleBorder(
        borderRadius: BorderRadius.zero,
      ),
      borderOnForeground: true,
      elevation: 0,
      margin: EdgeInsets.fromLTRB(0,0,0,0),
      child: ListTile(
        // ...
      ),
    )
    
    0 讨论(0)
  • 2020-12-15 04:03

    Screenshot:


    Solution:

    // you can do that by using `Map` but for simplicity I used 2 separate `List`. 
    List<int> _list = List.generate(20, (i) => i);
    List<bool> _selected = List.generate(20, (i) => false); // initially fill it up with false
    
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(),
        body: ListView.builder(
          itemBuilder: (_, i) {
            return Container(
              margin: const EdgeInsets.symmetric(vertical: 2),
              color: _selected[i] ? Colors.blue : null, // if current item is selected show blue color
              child: ListTile(
                title: Text("Item ${_list[i]}"),
                onTap: () => setState(() => _selected[i] = !_selected[i]), // reverse bool value
              ),
            );
          },
        ),
      );
    }
    
    0 讨论(0)
提交回复
热议问题