user-interface - 用户点击时如何更改文本颜色? Flutter-Firestore-StreamBuilder

标签 user-interface flutter dart google-cloud-firestore

我在Flustore上使用Flutter来收集选票,并且在UI方面很挣扎。
我想要的是当用户点击选项时,文本或边框上发生了一些变化...
This is what I have
This is what I need
要添加更多信息,这是我的代码:

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

    );
  }

最佳答案

尝试以下操作:

int selectedItem = -1; //添加您的状态

在您的列表中添加以下内容:

selectedItem == index
            ? Container(color: Colors.white)
            : Container(color: Colors.red));

而在
onTap: () {
 setState(() {
      selectedItem = selectedIndex;
    });
}

关于user-interface - 用户点击时如何更改文本颜色? Flutter-Firestore-StreamBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60338660/

相关文章:

Python Tkinter 子窗口问题

java - 是否可以使用字符串作为计算?

MATLAB 使用 uitab

dart - Flutter 应用程序卡住并且无法按预期工作

android - 验证程序未将数据保存到Firebase数据库

json - 如何在 dart/flutter 中正确序列化/反序列化 map ?

java - 在 WindowBuilder for Eclipse 中隐藏工具栏

Flutter:创建一个 add_close AnimatedIcons

android - Flutter 应用内购买,检查退款

dart - 运行后出错 "flutter upgrade"