我如何包装图像 url 的字幕,以便它引导到另一个屏幕?
How would I wrap the subtitle for the image url so that it leads to another screen?
我试图让用户点击图片并允许他们获取有关该图片的信息,我该怎么做?我正在考虑在手势检测器中包装字幕(带有图像 url),但出现了一些错误,所以我想知道如何修复此错误。谢谢
import 'package:flutter/material.dart';
class ProfilePage extends StatefulWidget {
const ProfilePage({Key? key}) : super(key: key);
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
// This holds a list of fiction users
// You can use data fetched from a database or a server as well
final List<Map<String, dynamic>> _allHerbs = [
{
"id": 1,
"name": "plant1",
"urlImage":
'https://www.southernexposure.com/media/products/originals/sweet-genovese-basil-809aaf7e3d9a3f3fa7ce2f0eb4480e95.jpg'
},
{"id": 2, "name": "plant2", "urlImage": ''},
{"id": 3, "name": "plant3", "urlImage": ''},
{"id": 4, "name": "plant4", "urlImage": ''},
{"id": 5, "name": "plant5", "urlImage": ''},
{"id": 6, "name": "plant6", "urlImage": ''},
{"id": 7, "name": "plant7", "urlImage": ''},
{"id": 8, "name": "plant8", "urlImage": ''},
{"id": 9, "name": "plant9", "urlImage": ''},
{"id": 10, "name": "plant10", "urlImage": ''},
];
// This list holds the data for the list view
List<Map<String, dynamic>> _foundHerbs = [];
@override
initState() {
// at the beginning, all users are shown
_foundHerbs = _allHerbs;
super.initState();
}
// This function is called whenever the text field changes
void _runFilter(String enteredKeyword) {
List<Map<String, dynamic>> results = [];
if (enteredKeyword.isEmpty) {
// if the search field is empty or only contains white-space, we'll display all users
results = _allHerbs;
} else {
results = _allHerbs
.where((user) =>
user["name"].toLowerCase().contains(enteredKeyword.toLowerCase()))
.toList();
// we use the toLowerCase() method to make it case-insensitive
}
// Refresh the UI
setState(() {
_foundHerbs = results;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Herb Search'),
),
body: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: [
const SizedBox(
height: 20,
),
TextField(
onChanged: (value) => _runFilter(value),
decoration: const InputDecoration(
labelText: 'Search', suffixIcon: Icon(Icons.search)),
),
const SizedBox(
height: 20,
),
Expanded(
child: _foundHerbs.isNotEmpty
? ListView.builder(
itemCount: _foundHerbs.length,
itemBuilder: (context, index) => Card(
key: ValueKey(_foundHerbs[index]["id"]),
color: Colors.blueAccent,
elevation: 4,
margin: const EdgeInsets.symmetric(vertical: 10),
child: ListTile(
leading: Text(
_foundHerbs[index]["id"].toString(),
style: const TextStyle(fontSize: 24),
),
title: Text(_foundHerbs[index]['name']),
subtitle: Image.Network('${_foundHerbs[index]["urlImage"]} '),
),
),
)
: const Text(
'No results found',
style: TextStyle(fontSize: 24),
),
),
],
),
),
);
}
}
首先,您不需要使用其他小部件来处理点击事件。 ListTile
已经有 onTap:
回调方法。
其次,您的 subtitle
有两个问题,Image.Network
将是 Image.network
并在 url 部分提供具有额外 space 的路径,或者你可以只使用 likeImage.network( _foundHerbs[index]["urlImage"],),
.
ListTile 会像
child: ListTile(
onTap: () {
print(_foundHerbs[index].toString());
},
leading: Text(
_foundHerbs[index]["id"].toString(),
style: const TextStyle(fontSize: 24),
),
title: Text(_foundHerbs[index]['name']),
subtitle: Container(
color: Colors.amber,
child: Image.network(
'${_foundHerbs[index]["urlImage"]}',
),
)),
我试图让用户点击图片并允许他们获取有关该图片的信息,我该怎么做?我正在考虑在手势检测器中包装字幕(带有图像 url),但出现了一些错误,所以我想知道如何修复此错误。谢谢
import 'package:flutter/material.dart';
class ProfilePage extends StatefulWidget {
const ProfilePage({Key? key}) : super(key: key);
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
// This holds a list of fiction users
// You can use data fetched from a database or a server as well
final List<Map<String, dynamic>> _allHerbs = [
{
"id": 1,
"name": "plant1",
"urlImage":
'https://www.southernexposure.com/media/products/originals/sweet-genovese-basil-809aaf7e3d9a3f3fa7ce2f0eb4480e95.jpg'
},
{"id": 2, "name": "plant2", "urlImage": ''},
{"id": 3, "name": "plant3", "urlImage": ''},
{"id": 4, "name": "plant4", "urlImage": ''},
{"id": 5, "name": "plant5", "urlImage": ''},
{"id": 6, "name": "plant6", "urlImage": ''},
{"id": 7, "name": "plant7", "urlImage": ''},
{"id": 8, "name": "plant8", "urlImage": ''},
{"id": 9, "name": "plant9", "urlImage": ''},
{"id": 10, "name": "plant10", "urlImage": ''},
];
// This list holds the data for the list view
List<Map<String, dynamic>> _foundHerbs = [];
@override
initState() {
// at the beginning, all users are shown
_foundHerbs = _allHerbs;
super.initState();
}
// This function is called whenever the text field changes
void _runFilter(String enteredKeyword) {
List<Map<String, dynamic>> results = [];
if (enteredKeyword.isEmpty) {
// if the search field is empty or only contains white-space, we'll display all users
results = _allHerbs;
} else {
results = _allHerbs
.where((user) =>
user["name"].toLowerCase().contains(enteredKeyword.toLowerCase()))
.toList();
// we use the toLowerCase() method to make it case-insensitive
}
// Refresh the UI
setState(() {
_foundHerbs = results;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Herb Search'),
),
body: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: [
const SizedBox(
height: 20,
),
TextField(
onChanged: (value) => _runFilter(value),
decoration: const InputDecoration(
labelText: 'Search', suffixIcon: Icon(Icons.search)),
),
const SizedBox(
height: 20,
),
Expanded(
child: _foundHerbs.isNotEmpty
? ListView.builder(
itemCount: _foundHerbs.length,
itemBuilder: (context, index) => Card(
key: ValueKey(_foundHerbs[index]["id"]),
color: Colors.blueAccent,
elevation: 4,
margin: const EdgeInsets.symmetric(vertical: 10),
child: ListTile(
leading: Text(
_foundHerbs[index]["id"].toString(),
style: const TextStyle(fontSize: 24),
),
title: Text(_foundHerbs[index]['name']),
subtitle: Image.Network('${_foundHerbs[index]["urlImage"]} '),
),
),
)
: const Text(
'No results found',
style: TextStyle(fontSize: 24),
),
),
],
),
),
);
}
}
首先,您不需要使用其他小部件来处理点击事件。 ListTile
已经有 onTap:
回调方法。
其次,您的 subtitle
有两个问题,Image.Network
将是 Image.network
并在 url 部分提供具有额外 space 的路径,或者你可以只使用 likeImage.network( _foundHerbs[index]["urlImage"],),
.
ListTile 会像
child: ListTile(
onTap: () {
print(_foundHerbs[index].toString());
},
leading: Text(
_foundHerbs[index]["id"].toString(),
style: const TextStyle(fontSize: 24),
),
title: Text(_foundHerbs[index]['name']),
subtitle: Container(
color: Colors.amber,
child: Image.network(
'${_foundHerbs[index]["urlImage"]}',
),
)),