用户的照片没有加载在flutter应用程序界面


При прохождении этого туториала я столкнулся с проблеммой, не отображаютсяфотографии пользователей 
[https://www.freecodecamp.org/news/build-a-chat-app-ui-with-flutter/][1]
[![this interface][2]][2]

[![structure of project][3]][3]

[1]: https://www.freecodecamp.org/news/build-a-chat-app-ui-with-flutter/
[2]: https://i.stack.imgur.com/32kwA.png
[3]: https://i.stack.imgur.com/Km8zp.png

home_page.dart

未标记的文件与教程中的文件完全相同

import'package:app_chat/screens/chat_page.dart';导入"package:flutter/material.dart";

class HomePage extends StatelessWidget {
const HomePage({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
body: const ChatPage(),
bottomNavigationBar: BottomNavigationBar(
selectedItemColor: Colors.red,
unselectedItemColor: Colors.grey.shade600,
selectedLabelStyle: const TextStyle(fontWeight: FontWeight.w600),
unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.w600),
type: BottomNavigationBarType.fixed,
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.message),
label: "Chats",
),
BottomNavigationBarItem(
icon: Icon(Icons.group_work),
label: "Channels",
),
BottomNavigationBarItem(
icon: Icon(Icons.account_box),
label: "Profile",
),
],
),
);
}
}

chat_page.dart

import 'package:app_chat/models/users_model.dart';
import 'package:app_chat/widgets/conversation_list.dart';
import 'package:flutter/material.dart';

class ChatPage extends StatefulWidget {
const ChatPage({super.key});

@override
_ChatPageState createState() => _ChatPageState();
}

用户参数已更改为用户模型文件中指定的参数

class _ChatPageState extends State<ChatPage> {
List<ChatUsers> chatUsers = [
ChatUsers(
name: "Jane Russel",
messageText: "Awesome Setup",
imageURL: "images/userImage1.jpeg",
time: "Now"),
ChatUsers(
name: "Glady's Murphy",
messageText: "That's Great",
imageURL: "images/userImage2.jpeg",
time: "Yesterday"),
ChatUsers(
name: "Jorge Henry",
messageText: "Hey where are you?",
imageURL: "images/userImage3.jpeg",
time: "31 Mar"),
ChatUsers(
name: "Philip Fox",
messageText: "Busy! Call me in 20 mins",
imageURL: "images/userImage4.jpeg",
time: "28 Mar"),
ChatUsers(
name: "Debra Hawkins",
messageText: "Thankyou, It's awesome",
imageURL: "images/userImage5.jpeg",
time: "23 Mar"),
ChatUsers(
name: "Jacob Pena",
messageText: "will update you in evening",
imageURL: "images/userImage6.jpeg",
time: "17 Mar"),
ChatUsers(
name: "Andrey Jones",
messageText: "Can you please share the file?",
imageURL: "images/userImage7.jpeg",
time: "24 Feb"),
ChatUsers(
name: "John Wick",
messageText: "How are you?",
imageURL: "images/userImage8.jpeg",
time: "18 Feb"),
];
@override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SafeArea(
child: Padding(
padding: const EdgeInsets.only(left: 16, right: 16, top: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
const Text(
"Conversations",
style:
TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
),
Container(
padding: const EdgeInsets.only(
left: 8, right: 8, top: 2, bottom: 2),
height: 30,
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 0.2),
borderRadius: BorderRadius.circular(5),
color: Colors.pink[50],
),
child: Row(
children: const <Widget>[
Icon(
Icons.add,
color: Colors.pink,
size: 20,
),
SizedBox(
width: 2,
),
Text(
"Add New",
style: TextStyle(
fontSize: 14, fontWeight: FontWeight.bold),
),
],
),
)
],
),
),
),
//Строка поиска
Padding(
padding: const EdgeInsets.only(top: 16, left: 16, right: 16),
child: TextField(
decoration: InputDecoration(
hintText: "Search...",
hintStyle: TextStyle(color: Colors.grey.shade600),
prefixIcon: Icon(
Icons.search,
color: Colors.grey.shade600,
size: 20,
),
filled: true,
fillColor: Colors.grey.shade100,
contentPadding: const EdgeInsets.all(8),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: BorderSide(color: Colors.grey.shade100)),
),
),
),
ListView.builder(
itemCount: chatUsers.length,
shrinkWrap: true,
padding: const EdgeInsets.only(top: 16),
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
return ConversationList(
name: chatUsers[index].name,
messageText: chatUsers[index].messageText,
imageUrl: chatUsers[index].imageURL,
time: chatUsers[index].time,
isMessageRead: (index == 0 || index == 3) ? true : false,
);
},
),
],
),
),
);
}
}

users_model.dart

参数名称已更改,如模型文件中所示

import'package:flutter/cupertino.dart';

class ChatUsers {
String name;
String messageText;
String imageURL;
String time;
ChatUsers(
{required this.name,
required this.messageText,
required this.imageURL,
required this.time});
}

conversation_list.dart
import 'package:flutter/material.dart';
class ConversationList extends StatefulWidget {
String name;
String messageText;
String imageUrl;
String time;
bool isMessageRead;
ConversationList(
{super.key, required this.name,
required this.messageText,
required this.imageUrl,
required this.time,
required this.isMessageRead});
@override
_ConversationListState createState() => 
_ConversationListState();
}
class _ConversationListState extends 
State<ConversationList> {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {},
child: Container(
padding: const EdgeInsets.only(left: 16, 
right: 16, top: 10, bottom: 10),
child: Row(
children: <Widget>[
Expanded(
child: Row(
children: <Widget>[
CircleAvatar(
backgroundImage: 
NetworkImage(widget.imageUrl),
maxRadius: 30,
),
const SizedBox(
width: 16,
),
Expanded(
child: Container(
color: Colors.transparent,
child: Column(
crossAxisAlignment: 
CrossAxisAlignment.start,
children: <Widget>[
Text(
widget.name,
style: const 
TextStyle(fontSize: 16),
),
const SizedBox(
height: 6,
),
Text(
widget.messageText,
style: TextStyle(
fontSize: 13,
color: 
Colors.grey.shade600,
fontWeight: 
widget.isMessageRead
? 
FontWeight.bold

: FontWeight.normal),
),
],
),
),
),
],
),
),
Text(
widget.time,
style: TextStyle(
fontSize: 12,
fontWeight: widget.isMessageRead
? FontWeight.bold
: FontWeight.normal),
),
],
),
),
);
}
}

pubspec.yaml

name: app_chat
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.0.0+1
environment:
sdk: '>=2.18.2 <3.0.0'
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^2.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter packages.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- images/userImage1.jpeg
- images/userImage2.jpeg
- images/userImage3.jpeg
- images/userImage4.jpeg
- images/userImage5.jpeg
- images/userImage6.jpeg
- images/userImage7.jpeg
- images/userImage8.jpeg
#   - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
#   - family: Schyler
#     fonts:
#       - asset: fonts/Schyler-Regular.ttf
#       - asset: fonts/Schyler-Italic.ttf
#         style: italic
#   - family: Trajan Pro
#     fonts:
#       - asset: fonts/TrajanPro.ttf
#       - asset: fonts/TrajanPro_Bold.ttf
#         weight: 700
#

有关来自包依赖项的字体的详细信息

参见https://flutter.dev/custom-fonts/#from-包装

我假设您将文件复制到assets/images/文件夹中,对吗?

此外,您还需要将您的图像引用到pubspec.yaml文件中,如下所示:

flutter:
...
assets:
- assets/images/example.png

相关内容

  • 没有找到相关文章

最新更新