首页 > 解决方案 > 如何获取 TextWidget onTap 的文本

问题描述

我有一个在文本小部件中显示日期的 ListView。在点击文本小部件时,我想要字符串中的文本。我怎么能得到那个?

这是我的代码。

 children: <Widget>[
    GestureDetector(
      onTap: () {
         print('OnTapped');
         print(arrayDay[i].toString());
         // get the text of TextWidget here
      },
      child: new Text(
         arrayDay[i].toString(),
         style: new TextStyle(
         fontWeight: FontWeight.w400),
        ),
     ),

标签: androidiosflutter

解决方案


在点击方法中,您可以从列表中获取数据。所以我在这里发布了一些代码,所以得到更多的想法

return ListView.builder(
            itemCount: count,
            itemBuilder: (BuildContext context, int position) {
              return Card(
                color: Colors.white,
                elevation: 2.0,
                child: ListTile(

                  title: Text(this.noteList[position].title,
                    style: titleStyle,
                  ),
                  subtitle: Text(this.noteList[position].date),

                  onTap: () {
                    debugPrint('Note Data '+ this.noteList[position].title);
                    debugPrint('Data '+ 'Here your Data Get'); // you can get data here
                  },
                ),
              ); // Card
            });

推荐阅读