我是Bloc模式的新手,因此在更新用image_picker选择的图像时遇到了一些麻烦。
到目前为止,这是我的代码
image_picker_provider.dart
import 'dart:io';
import 'package:image_picker/image_picker.dart';
class ImagePickerProvider {
Future<File> getImage() async {
File image = await ImagePicker.pickImage(
source: ImageSource.gallery, maxWidth: 150.0, maxHeight: 150.0);
return image;
}
Future<File> takeImage() async {
var image = await ImagePicker.pickImage(
source: ImageSource.camera, maxWidth: 150.0, maxHeight: 150.0);
return image;
}
}
repository.dart
Future<File> getImage() => _imagePickerProvider.getImage();
Future<File> takeImage() => _imagePickerProvider.takeImage();
form.dart
Widget picture() {
return StreamBuilder(
stream: _bloc.profilePicture,
builder: (BuildContext context, AsyncSnapshot<Future<File>> snapshot) {
if (!snapshot.hasData || snapshot.hasError) {
return defaultPicture();
} else {
return profilePicture(snapshot);
}
});
}
Widget defaultPicture() {
return Container(
width: 150.0,
height: 150.0,
decoration: new BoxDecoration(
image: new DecorationImage(
image: AssetImage("assets/defaultProfile.jpg"),
fit: BoxFit.cover,
),
borderRadius: new BorderRadius.all(new Radius.circular(128.0)),
border: new Border.all(
color: Colors.black,
width: 1.0,
),
),
);
}
Widget profilePicture(File snapshotData) {
return Container(
width: 150.0,
height: 150.0,
decoration: new BoxDecoration(
image: new DecorationImage(
image: FileImage(snapshotData),
fit: BoxFit.cover,
),
borderRadius: new BorderRadius.all(new Radius.circular(128.0)),
border: new Border.all(
color: Colors.black,
width: 1.0,
),
),
);
}
到目前为止,我所拥有的关于个人资料图片的所有信息
create_account_bloc.dart
import 'dart:async';
import 'dart:io';
import 'package:rxdart/rxdart.dart';
import '../resources/repository.dart';
class CreateAccountBloc {
final _repository = Repository();
final _email = BehaviorSubject<String>();
final _password = BehaviorSubject<String>();
final _firstName = BehaviorSubject<String>();
final _phoneNumber = BehaviorSubject<int>();
final _profilePicture = BehaviorSubject<Future<File>>();
final _profilePictureURL = BehaviorSubject<String>();
final _createAccountStatus = BehaviorSubject<CreateAccountStatus>();
Observable<String> get email => _email.stream.transform(_validateEmail);
Observable<String> get password =>
_password.stream.transform(_validatePassword);
Observable<String> get firstName =>
_firstName.stream.transform(_validateFirstName);
Observable<CreateAccountStatus> get createAccountStatus =>
_createAccountStatus.stream;
Observable<Future<File>> get profilePicture => _profilePicture.stream;
String get emailAddress => _email.value;
// Change data
Function(String) get changeEmail => _email.sink.add;
Function(String) get changePassword => _password.sink.add;
Function(String) get changeFirstName {
return _firstName.sink.add;
}
Function(CreateAccountStatus) get changeAccountStatus =>
_createAccountStatus.sink.add;
final _validateEmail =
StreamTransformer<String, String>.fromHandlers(handleData: (email, sink) {
if (Validator.isValidEmail(email)) {
sink.add(email);
} else {
sink.addError(StringConstant.emailValidateMessage);
}
});
final _validatePassword = StreamTransformer<String, String>.fromHandlers(
handleData: (password, sink) {
if (password.length >= 6) {
sink.add(password);
} else {
sink.addError(StringConstant.passwordValidateMessage);
}
});
final _validateFirstName = StreamTransformer<String, String>.fromHandlers(
handleData: (firstName, sink) {
if (Validator.isValidFirstName(firstName)) {
sink.add(firstName);
} else {
sink.addError(StringConstant.firstNameValidateMessage);
}
});
bool validateFields() {
if (Validator.isNotNullEmptyFalseOrZero(_email.value) &&
Validator.isNotNullEmptyFalseOrZero(_password.value) &&
Validator.isValidFirstName(_firstName.value) &&
// _phoneNumber.value != null &&
//// _phoneNumber.value.is &&
// _profilePicture.value != null &&
// _profilePicture.value.isNotEmpty &&
Validator.isValidEmail(_email.value) &&
_password.value.length >= 6) {
return true;
} else {
return false;
}
}
void dispose() async {
await _email.drain();
_email.close();
await _password.drain();
_password.close();
await _firstName.drain();
_firstName.close();
await _phoneNumber.drain();
_phoneNumber.close();
await _profilePicture.drain();
_profilePicture.close();
await _profilePictureURL.drain();
_profilePictureURL.close();
await _createAccountStatus.drain();
_createAccountStatus.close();
}
}
我试图复制其他部分并使它们正常工作,但是每次旅途中我似乎都会遇到错误。
关于如何从图库中选择图像并更新_bloc.profilePicture
的任何想法?
谢谢!
答案 0 :(得分:0)
我不太确定您拥有什么,还没有尝试过。
但是我能够使您的代码像这样
create_account_bloc.dart
final _profilePicture = BehaviorSubject<File>();
Observable<File> get profilePicture => _profilePicture.stream;
Function(File) get changeProfilePicture => _profilePicture.sink.add;
void getImage() {
_repository.getImage().then((value) {
_profilePicture.sink.add(value);
});
}
void takeImage() {
_repository.takeImage().then((value) {
_profilePicture.sink.add(value);
});
}