Listtile多选-Flutter Firestore Streambuilder

时间:2020-02-23 18:40:53

标签: flutter dart google-cloud-firestore

我需要帮助来构建带有抖动的测验应用程序, 我将Firestore用作我的数据,并且我想添加一个多选问题,因此,当用户点击一个选择时,该选择将突出显示,例如以下示例

(我从另一个问题中使用此gif,因为我不知道如何解释)

这就是我现在拥有的

这是我的代码:

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 小部件build(BuildContext context){ 返回脚手架( 正文:容器(

    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"),
  ),

);

}

非常感谢您!

1 个答案:

答案 0 :(得分:0)

用彩色容器包装列表磁贴:

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

在粘贴内容时更改选择状态。

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

DartPad上尝试