0
votes

i need help to build a quiz app with flutter, i use firestore for my data, and i want to add a multi choices question, so when a user tap on one choice, this one is highlighted, like this example

(i use this gif from another question, because i didn't know how to explain)

this is what i have for now

this is my code :

Widget _buildListItem(BuildContext context, DocumentSnapshot document) {

return ListTile(
  title: Container(
    margin: EdgeInsets.all(8.0),
    padding: EdgeInsets.fromLTRB(210, 0.0, 0.0, 0.0),
    decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(
            color: Colors.pink[800], // set border color
            width: 3.0), // set border width
        borderRadius: BorderRadius.all(
            Radius.circular(10.0)), // set rounded corner radius
        boxShadow: [
          BoxShadow(
              blurRadius: 5,
              color: Colors.black,
              offset: Offset(0.5, 1))
        ] // make rounded corner of border
        ),
      child: Row(
          children: <Widget>[
        Container(
            child: Text(
              document['rep'],
              style: TextStyle(
                fontSize: 50.0,
                color: Colors.black,
              ),
            ),
        )

          ]
      ),
  ),

  onTap: () {
    Firestore.instance.runTransaction(
            (transaction) async {
      DocumentSnapshot freshSnap =
      await transaction.get(document.reference);
      await transaction.update(freshSnap.reference, {
        'votes': freshSnap['votes'] + 1,
      });
    });


  },

);

}

@override Widget build(BuildContext context) { return Scaffold( body: Container(

    child: StreamBuilder(
        stream: Firestore.instance.collection('questions').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) return const Text('Loading ...');
          return ListView.builder(
              padding: EdgeInsets.fromLTRB(50.0, 300.0, 50.0, 0.0),
              itemExtent: 100.0,
              itemCount: snapshot.data.documents.length,
              itemBuilder: (context, index) =>
                  _buildListItem(context, snapshot.data.documents[index]),
            );

        }),

  ),

  floatingActionButton: FloatingActionButton(
    onPressed: () {
      Navigator.push(
          context, MaterialPageRoute(builder: (context) => new Home()));
    },
    child: Text("Home"),
  ),

);

}

thank you so much !

1

1 Answers

1
votes

Wrap list tile with colored container :

itemBuilder: (context, index){
 return Container(
     color: isSelected[index] ? Colors.blue : null,
     child: ListTile(title:'test'),
 );
}                  

Change selection status when item is taped.

ListTile(
    title: Text('test'),
    selected: isSelected[index],
    onTap: () {
      setState(() {
        isSelected[index] = !isSelected[index];
      });
    },
),
final List<bool> isSelected;

Try it on DartPad