Flutter 如何使从json动态接收的文本片段在颤振中可点击?
我想知道是否有任何方法(实现、包)可以用来使动态接收字符串的特定部分可单击 例如,让我们来看看这个JSON文件。 我想知道,当我在一个包含所有用户的列表生成器中显示这个用户后,我是否可以在突出显示的“客户机-服务器”单词上有一个可点击的平面按钮,这样当我按下它时,我就可以导航到一个单独的字典页面,并进一步搜索该单词 我的意思是,我如何标记“客户机-服务器”这个词,以便颤振可以帮助我在构建列表时动态单击它?我有办法做到吗?特别是能够有一个索引或id附加到它,以便我可以在另一个屏幕上使用它 作为一个解决方案,我想知道我是否可以在json文件中用一些按钮标记(如html(客户端服务器))包装“客户端-服务器”这个词,当我将字符串设置为flatter小部件时,它可以以某种方式用id或名称识别它,这样当我按下它时,我可以将名称或/和id传递给在字典中搜索它的导航器,但坦率地说,正如我在web上搜索过的那样,没有什么可以做这种行为 提前感谢您抽出时间阅读我的问题。Flutter 如何使从json动态接收的文本片段在颤振中可点击?,flutter,Flutter,我想知道是否有任何方法(实现、包)可以用来使动态接收字符串的特定部分可单击 例如,让我们来看看这个JSON文件。 我想知道,当我在一个包含所有用户的列表生成器中显示这个用户后,我是否可以在突出显示的“客户机-服务器”单词上有一个可点击的平面按钮,这样当我按下它时,我就可以导航到一个单独的字典页面,并进一步搜索该单词 我的意思是,我如何标记“客户机-服务器”这个词,以便颤振可以帮助我在构建列表时动态单击它?我有办法做到吗?特别是能够有一个索引或id附加到它,以便我可以在另一个屏幕上使用它 作为一
列出产品=[];
List<Map> products = [];
@override
void initState() {
super.initState();
Map tmp = {
"products": [
{
"prod_id": "prod-345",
"name": "Something",
"category": "Cloth",
"thumbnail": "",
"desc": 'An example of description',
"click_on": 'example',
'action': 'https://google.com'
},
{
"prod_id": "prod-123",
"name": "Something Else",
"category": "Watch",
"thumbnail": "",
"desc": '2nd example of description',
"click_on": 'description',
'action': 'second_screen'
},
]
};
products.addAll(tmp['products']);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Let\'s parse some JSON'),
),
body: Container(
padding: EdgeInsets.fromLTRB(5, 10, 5, 10),
child: ListView.builder(
itemCount: products.length,
itemBuilder: (cc, ind) {
return ListTile(
title: Text(products[ind]['name']),
subtitle: Text.rich(TextSpan(
text: products[ind]['desc'].toString().substring(
0,
products[ind]['desc'].toString().indexOf(
products[ind]['click_on'].toString())),
children: <TextSpan>[
TextSpan(
//if you want style !
style:TextStyle(
decoration:TextDecoration.underline,
),
text: products[ind]['click_on'],
recognizer: new TapGestureRecognizer()
..onTap = () => print('Tap Here onTap'),
),
TextSpan(
text: products[ind]['desc'].toString().substring(
products[ind]['desc'].toString().indexOf(
products[ind]['click_on']
.toString()) +
products[ind]['click_on']
.toString()
.length,
products[ind]['desc'].toString().length),
)
])));
})));
}
@凌驾
void initState(){
super.initState();
映射tmp={
“产品”:[
{
“产品id”:“prod-345”,
“名字”:“某物”,
“类别”:“布料”,
“缩略图”:“,
“desc”:“描述示例”,
“点击”:“示例”,
'行动':'https://google.com'
},
{
“产品id”:“prod-123”,
“名称”:“其他东西”,
“类别”:“手表”,
“缩略图”:“,
“描述”:“第二个描述示例”,
“单击”:“说明”,
“操作”:“第二个屏幕”
},
]
};
addAll(tmp['products');
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:const Text('Let's parse some JSON'),
),
主体:容器(
填充:从LTRB(5,10,5,10)开始的边缘设置,
子项:ListView.builder(
itemCount:products.length,
项目生成器:(抄送,索引){
返回列表块(
标题:文本(产品[ind][name]],
字幕:Text.rich(TextSpan(
text:products[ind]['desc'].toString()子字符串(
0,
产品[ind]['desc'].toString().indexOf(
产品[ind]['点击].toString()),
儿童:[
TextSpan(
//如果你想要时尚!
样式:TextStyle(
装饰:textEdition.underline,
),
文本:产品[ind][“点击”],
识别器:新的TapGestureRecognizer()
…onTap=()=>打印('Tap Here onTap'),
),
TextSpan(
text:products[ind]['desc'].toString()子字符串(
产品[ind]['desc'].toString().indexOf(
产品[ind][“点击”]
.toString())+
产品[ind][“点击”]
.toString()
.长度,
产品[ind]['desc'].toString().length),
)
])));
})));
}
您不明白。我想让一些特定的单词在句子中可以点击。您正在使json的特定字段可单击。在您的示例中,如果我在每个产品中都有一个描述字段,上面写着“description”:“这是一个名为Alex's store的更复杂的商店的一部分”,我希望Alex's store能够以某种方式单击。我不知道如何将该词标记为flatter,以及flatter如何识别该标记以使其可点击。谢谢您更新的答案!但是,如果我在“单击”字段中有多个单词可以单击到“描述”字段中,该怎么办?考虑到它们是动态构建的,我如何判断按下的是哪一个呢?在副标题处使用textspan儿童循环(在上面的例子中)