如何确保我在颤动中的图像 url 不会显示在屏幕上而不是图像上?
Posted
技术标签:
【中文标题】如何确保我在颤动中的图像 url 不会显示在屏幕上而不是图像上?【英文标题】:How do I ensure that the url of my image in flutter doesn't show up on the screen instead of the image? 【发布时间】:2021-12-17 11:45:27 【问题描述】:我是新来的颤振。我试图让一些植物的图片出现在特定植物的名称旁边,但是我得到的是 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: Text('$_foundHerbs[index]["urlImage"] '),
),
),
)
: const Text(
'No results found',
style: TextStyle(fontSize: 24),
),
),
],
),
),
);
【问题讨论】:
【参考方案1】:您希望从您只需要的文本小部件中获取图像
改变这个
subtitle: Text('$_foundHerbs[index]["urlImage"] '),
到这里
subtitle: Image.network('$_foundHerbs[index]["urlImage"] '),
上面将在标题下显示您的图像,但如果您想在旁边显示它,请使用前导或尾随而不是副标题
喜欢这个
leading: Image.network('$_foundHerbs[index]["urlImage"] '),
【讨论】:
点击图片时如何显示图片信息? 列表图块具有点击功能,因此您可以使用它并传递内容以显示更多信息以上是关于如何确保我在颤动中的图像 url 不会显示在屏幕上而不是图像上?的主要内容,如果未能解决你的问题,请参考以下文章