gpt4 book ai didi

android-studio - 在Flutter中显示ListView的内容并显示从图库上传的图像

转载 作者:行者123 更新时间:2023-12-03 04:43:39 26 4
gpt4 key购买 nike

我对使用Flutter / Dart的软件开发人员还很陌生,并且我目前正在开发一个使用Listview来显示用户输入的信息的小部件。该信息当前存储在列表中,由于某种原因,每次我向列表中添加新信息时,仅打印第一个索引处的信息。我实际上没有收到任何错误消息,因此很难跟踪它。我曾尝试使用打印语句对其进行调试,但实际上并没有帮助。
第二个问题是,当用户从图库中选择图片时,不会立即显示该图片。
我该如何解决这些问题?

    import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:mind_matters/shared/loading.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart';

class EmergencyContacts extends StatefulWidget {

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

class _EmergencyContactsState extends State<EmergencyContacts> {
bool loading = false;
static bool buttonPressed = true;

static String emergencyName = "";
static String emailAddress = "";
static String phoneNumber = "";
static File _contactPic;

static int index = -1;

Future getPic() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);

setState(() {
_contactPic = image;
print('Image path is: $_contactPic');
});
}

Future uploadPic(BuildContext context) async {
String fileName = basename(_contactPic.path);
StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = firebaseStorageRef.putFile(_contactPic);
StorageTaskSnapshot takeSnapshot = await uploadTask.onComplete;
}

List<EmergencyContact> emergencyContacts = [];

void createInstanceOfContact() {
if (emergencyName.isNotEmpty) {
emergencyContacts.add(EmergencyContact(
contactPic: _contactPic,
emergencyName: emergencyName,
phoneNumber: phoneNumber,
emailAddress: emailAddress));
} else if (emergencyName.isEmpty) {
emergencyContacts.add(EmergencyContact(
contactPic: null,
emergencyName: "null",
phoneNumber: "null",
emailAddress: "null"));
}
}

void incrementIndex() {
if (buttonPressed){
index++;
}
}

@override
Widget build(BuildContext context) {
return loading
? Loading()
: Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xffff696A),
elevation: 0,
leading: new IconButton(
icon: new Icon(Icons.arrow_back, color: Colors.black45),
onPressed: () => Navigator.of(context).pop(),
),
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Stack(
alignment: Alignment.center,
children: <Widget>[
Text(" Add emergency contacts",
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Poppins',
color: Colors.black54,
fontSize: 20,
fontWeight: FontWeight.bold,
)),
],
),
],
),
automaticallyImplyLeading: false,
centerTitle: true,
),
body: Container(
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [const Color(0xffff696A), const Color(0xffca436b)],
begin: FractionalOffset.topLeft,
end: FractionalOffset.bottomRight,
stops: [0.0, 1.0],
tileMode: TileMode.clamp),
),
child: SingleChildScrollView(
child: Container(
height: 100000,
child: Padding(
padding: EdgeInsets.fromLTRB(20, 0, 20, 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
SizedBox(height: 20),
Container(
child: Padding(
padding: EdgeInsets.all(10),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
"For your safety, please enter at least 1 phone number "
"and email. "
"It will be used to contact someone if we or "
"someone you're talking to feel you need "
"immediate help.",
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 16,
)),
SizedBox(height: 20),
new ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (context, index) =>
new CardRow(
emergencyContacts[index]),
itemCount: emergencyContacts.length,
),
Text("the index here is $index"),
SizedBox(height: 10),
Align(
alignment: Alignment.bottomCenter,
child: Container(
width: 194,
child: FloatingActionButton(
onPressed: () {
setState(() {
_contactPic = null;
emergencyName = "";
emailAddress = "";
phoneNumber = "";
});
showAlertDialog(context);
},
child: Icon(
Icons.add,
),
foregroundColor: Colors.white,
backgroundColor:
const Color(0xffff696A),
mini: true,
elevation: 10,
),
),
),
SizedBox(height: 10),
Container(
width: 106,
child: RaisedButton(
shape: new RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(
18.0),
side: BorderSide(
color: const Color(0xffff696A),
)),
elevation: 10,
//:todo don't forget on pressed action
onPressed: () async {
},
color: const Color(0xffff696A),
textColor: Colors.white,
child: Row(children: <Widget>[
Text("next step".toUpperCase(),
textAlign: TextAlign.center,
style: TextStyle(fontSize: 14)),
]),
),
),
],
),
),
),
),
])),
),
),
));
}

showAlertDialog(BuildContext context) {

// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return Dialog(
child: Container(
height: 422,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
//mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Add Contact",
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Poppins',
color: const Color(0xffff696A),
fontSize: 20,
fontWeight: FontWeight.bold,
)),
SizedBox(height: 10),
CircleAvatar(
radius: 50,
backgroundColor: const Color(0xffff696A),
child: ClipOval(
child: SizedBox(
width: 92,
height: 92,
child: (_contactPic != null)
? Image.file(_contactPic, fit: BoxFit.fill)
: Image.network(
'IMAGE',
fit: BoxFit.fill,
)),
),
),
Padding(
padding: EdgeInsets.only(left: 26),
child: IconButton(
icon: Icon(
Icons.photo_camera,
size: 30,
),
onPressed: () {
getPic();
},
),
),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => emergencyName = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.person,
),
hintText: "Enter their name/nickname",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
SizedBox(height: 10),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => emailAddress = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.email,
),
hintText: "Enter their email address",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
SizedBox(height: 10),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => phoneNumber = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.phone,
),
hintText: "Enter their phone number",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
Align(
alignment: Alignment.center,
child: Row(
children: <Widget>[
FlatButton(
child: Text("Cancel",
style: TextStyle(
color: const Color(0xffff696A),
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
onPressed: () {
Navigator.of(context).pop();
},
),
SizedBox(width: 80),
FlatButton(
child: Text("Add Contact",
style: TextStyle(
color: const Color(0xffff696A),
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
onPressed: () async {
setState(() {
buttonPressed = true;
});
if (buttonPressed){
createInstanceOfContact();
incrementIndex();
}
print("the index is $index");
print(emergencyContacts[index].contactPic.toString());
print(emergencyContacts[index].emergencyName);
print(emergencyContacts[index].phoneNumber);
print(emergencyContacts[index].emailAddress);

Navigator.of(context).pop();

print(_contactPic.toString());
print(emergencyName);
print(emailAddress);
print(phoneNumber);
},
),
],
),
)
],
),
),
),
),
);
});
}
}

class CardRow extends StatelessWidget {
static EmergencyContact emergencyContact;
static File emergencyContactPic = _EmergencyContactsState._contactPic;
static String emergencyName = _EmergencyContactsState.emergencyName;
static String emergencyNumber = _EmergencyContactsState.phoneNumber;
static String emergencyEmail = _EmergencyContactsState.emailAddress;

CardRow(emergencyContact);

@override
Widget build(BuildContext context) {
return new Container(
height: 130,
margin: const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
child: new Stack(
children: <Widget>[
contactCard,
contactCardContent,
contactThumbnail,
deleteContact,
],
));
}

final contactThumbnail = new Container(
//margin: new EdgeInsets.only(top: 3, left: 270),
margin: new EdgeInsets.only(bottom: 5),
alignment: FractionalOffset.centerLeft,
child: CircleAvatar(
radius: 46,
backgroundColor: new Color(0xFF733b67),
child: ClipOval(
child: SizedBox(
width: 82,
height: 82,
child: (emergencyContactPic != null)
? Image.file(emergencyContactPic, fit: BoxFit.fill)
: Image.network(
'IMAGE',
fit: BoxFit.fill,
)),
)),
);

final contactCard = new Container(
height: 124,
margin: new EdgeInsets.only(left: 46),
decoration: new BoxDecoration(
color: new Color(0xFF733b67),
shape: BoxShape.rectangle,
borderRadius: new BorderRadius.circular(8),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Colors.black12,
blurRadius: 10,
offset: new Offset(0, 10),
)
]),
);

final deleteContact = new Container(
margin: new EdgeInsets.only(top: 3, left: 270),
child: IconButton(
icon: new Icon (Icons.delete_sweep,
size: 30,
),
color: const Color(0xffff696A),
onPressed: (){
//todo delete card
},
),
);

final contactCardContent = new Container(
margin: new EdgeInsets.fromLTRB(90, 16, 16, 16),
constraints: new BoxConstraints.expand(),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(height: 4.0),
new Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.person,
size: 20,
),
new SizedBox(width: 10.0),
new Text(emergencyName,
style: TextStyle(
color: Colors.white,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
]),
new SizedBox(height: 10.0),
Row(children: <Widget>[
Icon(
Icons.phone,
size: 20,
),
new SizedBox(width: 10.0),
Text(emergencyNumber,
style: TextStyle(
color: Colors.grey,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
]),
new Container(
margin: new EdgeInsets.symmetric(vertical: 8.0),
height: 2.0,
width: 18.0,
color: const Color(0xffff696A)),
new Row(
children: <Widget>[
Icon(
Icons.mail,
size: 20,
),
new SizedBox(width: 10.0),
new Text(emergencyEmail,
style: TextStyle(
color: Colors.grey,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
],
),
],
),
);
}

class EmergencyContact {
File contactPic;
String emergencyName;
String phoneNumber;
String emailAddress;

EmergencyContact(
{this.contactPic,
this.emergencyName,
this.phoneNumber,
this.emailAddress});
}

最佳答案

listView.builder无法正常运行的原因是,您已在CardRow窗口小部件中声明了变量,并使用了这些静态值而不是您传递的参数。您正在传递参数,只是不使用它们。
我在这里所做的唯一更改:

final EmergencyContact emergencyContact;
//static File emergencyContactPic = _EmergencyContactsState._contactPic;
// static String emergencyName = _EmergencyContactsState.emergencyName;
// static String emergencyNumber = _EmergencyContactsState.phoneNumber;
// static String emergencyEmail = _EmergencyContactsState.emailAddress;

CardRow(this.emergencyContact);
在这里,我将您的小部件提取为方法(只是在参数中传递的方法),之所以这样做,是因为在完成的初始化程序中无法访问 this。我建议您更多地开始使用 final,并将您的Widget提取到它们自己的有状态/无状态Widget中,或者将其提取为方法,如下文所述。这是一个好习惯。
child: new Stack(
children: <Widget>[
contactCard,
contactCardContent(),
contactThumbnail(),
deleteContact,
],
));


Container contactThumbnail() {
return Container(
//margin: new EdgeInsets.only(top: 3, left: 270),
margin: new EdgeInsets.only(bottom: 5),
alignment: FractionalOffset.centerLeft,
child: CircleAvatar(
radius: 46,
backgroundColor: new Color(0xFF733b67),
child: ClipOval(
child: SizedBox(
width: 82,
height: 82,
child: (emergencyContact.contactPic != null)
? Image.file(emergencyContact.contactPic, fit: BoxFit.fill)
: Image.network(
'IMAGE',
fit: BoxFit.fill,
)),
)),
);
}

Container contactCardContent() {
return new Container(
margin: new EdgeInsets.fromLTRB(90, 16, 16, 16),
constraints: new BoxConstraints.expand(),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(height: 4.0),
new Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.person,
size: 20,
),
new SizedBox(width: 10.0),
new Text(emergencyContact.emergencyName,
style: TextStyle(
color: Colors.white,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
]),
new SizedBox(height: 10.0),
Row(children: <Widget>[
Icon(
Icons.phone,
size: 20,
),
new SizedBox(width: 10.0),
Text(emergencyContact.phoneNumber,
style: TextStyle(
color: Colors.grey,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
]),
new Container(
margin: new EdgeInsets.symmetric(vertical: 8.0),
height: 2.0,
width: 18.0,
color: const Color(0xffff696A)),
new Row(
children: <Widget>[
Icon(
Icons.mail,
size: 20,
),
new SizedBox(width: 10.0),
new Text(emergencyContact.emailAddress,
style: TextStyle(
color: Colors.grey,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
],
),
],
),
);
}
第二个问题
对于第二个问题,您没有看到立即显示图像的原因是未重建 showDialog。为了对此进行更多说明, setState函数中使用的 showAlertDialog属于父窗口小部件,因此,这意味着它将仅重建该特定函数之外的内容。仅当您第一次单击showAlertDialog函数时,该函数才会重新构建。为了解决这个问题,我们要么必须将showDialog放在有状态的小部件中,要么使用StatefulBuilder( https://api.flutter.dev/flutter/widgets/StatefulBuilder-class.html)
StatefulBuilder具有自己的setState构造,这意味着现在可以在使用setState时对其进行重建,从而立即显示图像。但是现在我们还有另一个问题,我看到您那里有boolean buttonPressed,整个setState(boolean)属于父窗口小部件,而不是StatefulBuilder setState,所以现在这意味着该列表将不会在“添加联系人”之后显示一直按直到被重建。我们需要来自父级而不是StatefulBuilder的setState。有很多方法可以解决此问题,但是我个人认为使用回调是最好的方法之一。首先,您将需要创建一个函数,例如rebuildWidget,然后只需在此处更改状态即可。其次,将该函数作为参数传递给showAlertDialog。第三,继续创建最终功能,然后分配它。最后,只需在onPressed上调用它即可。
void rebuildWidget() {
setState(() {
buttonPressed = true;
});
}


showAlertDialog(
context,
rebuildWidget
);

showAlertDialog(BuildContext context, myState) {

final Function() updateParent = myState;

showDialog(
context: context,
builder: (context) {
String contentText = "Content of Dialog";
File myPic;
return StatefulBuilder(
builder: (context, setState) {
return Dialog(
// title: Text("Title of Dialog"),
child: Container(
height: 422,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
//mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Add Contact",
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Poppins',
color: const Color(0xffff696A),
fontSize: 20,
fontWeight: FontWeight.bold,
)),
SizedBox(height: 10),
CircleAvatar(
radius: 50,
backgroundColor: const Color(0xffff696A),
child: ClipOval(
child: SizedBox(
width: 92,
height: 92,
child: myPic != null
? Image.file(myPic, fit: BoxFit.fill)
: Image.network(
'IMAGE',
fit: BoxFit.fill,
)),
),
),
Padding(
padding: EdgeInsets.only(left: 26),
child: IconButton(
icon: Icon(
Icons.photo_camera,
size: 30,
),
onPressed: () async {
await getPic();
setState(() { // To rebuild this method
myPic = _contactPic;
});
},
),
),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => emergencyName = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.person,
),
hintText: "Enter their name/nickname",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
SizedBox(height: 10),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => emailAddress = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.email,
),
hintText: "Enter their email address",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
SizedBox(height: 10),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => phoneNumber = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.phone,
),
hintText: "Enter their phone number",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
Align(
alignment: Alignment.center,
child: Row(
children: <Widget>[
FlatButton(
child: Text("Cancel",
style: TextStyle(
color: const Color(0xffff696A),
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
onPressed: () {
Navigator.of(context).pop();
},
),
SizedBox(width: 80),
FlatButton(
child: Text("Add Contact",
style: TextStyle(
color: const Color(0xffff696A),
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
onPressed: () async {
updateParent();

if (buttonPressed) {
createInstanceOfContact();
incrementIndex();
}
print("the index is $index");
print(emergencyContacts[index]
.contactPic
.toString());
print(emergencyContacts[index].emergencyName);
print(emergencyContacts[index].phoneNumber);
print(emergencyContacts[index].emailAddress);

Navigator.of(context).pop();

print(_contactPic.toString());
print(emergencyName);
print(emailAddress);
print(phoneNumber);
},
),
],
),
)
],
),
),
),
),
);
},
);
},
);
}
}

关于android-studio - 在Flutter中显示ListView的内容并显示从图库上传的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62678507/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com