【问题标题】:Listtile Multi-select - Flutter Firestore StreambuilderListtile 多选 - Flutter Firestore Streambuilder
【发布时间】:2020-06-07 11:18:23
【问题描述】:

我需要帮助来构建一个带有颤振的测验应用程序, 我为我的数据使用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,
      });
    });


  },

);

}

@覆盖 小部件构建(BuildContext 上下文){ 返回脚手架( 正文:容器(

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

);

}

非常感谢!

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    用彩色容器包装列表图块:

    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

    【讨论】:

    • 你能解释一下吗!
    • 没问题~~~~
    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2020-10-03
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2021-02-26
    相关资源
    最近更新 更多