在我的Flutter应用程序中,(Github link here)出现一些不一致的行为。当我在模拟器中运行该应用程序时,我可以从图像库中选择一个图像(使用 image_picker 包),然后在屏幕上显示我选择的图像。
但是,当我构建应用程序并将.apk文件安装到手机上时,从画廊中选择图像后,图像只会显示为空白白色。
“ dora the Explorer gif”应该位于图像的后面。
import 'dart:io';
import 'dart:async';
import 'package:ShowPhotoLock/core/constants/app_strings.dart';
import 'package:ShowPhotoLock/core/viewmodels/views/baseWidget.dart';
import 'package:ShowPhotoLock/core/viewmodels/views/drawer/drawerOnly.dart';
import 'package:ShowPhotoLock/core/viewmodels/views/home/homeScreenViewModel.dart';
import 'package:ShowPhotoLock/core/viewmodels/widgets/photoViewWrapper.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:provider/provider.dart';
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
Future<File> _imageFile;
pickImageFromGallery(ImageSource source) {
setState(() {
_imageFile = ImagePicker.pickImage(source: source);
});
}
Widget showImage() {
return FutureBuilder<File>(
future: _imageFile,
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
snapshot.data != null) {
return Center(child: Image.file(snapshot.data, width: 300, height: 300));
} else if (snapshot.error != null) {
return const Text(
'Error Picking Image',
textAlign: TextAlign.center,
);
}
return Center(
child: Text( 'No Image',
style: Theme.of(context).textTheme.body1)
);
},
);
}
@override
Widget build(BuildContext context) {
return BaseWidget<HomeScreenViewModel>(
model: HomeScreenViewModel(sharedPreferencesService: Provider.of(context)),
builder: (context, model, child) => Scaffold(
appBar: AppBar(
title: Text(
'Show Photo Lock',
style: Theme.of(context).textTheme.headline,
),
),
drawer: _imageFile == null ? DrawerOnly() : null,
body: _imageFile == null ? Container() : showImage(),
floatingActionButton: _imageFile == null
? FloatingActionButton(
onPressed: () {
pickImageFromGallery(ImageSource.gallery);
},
tooltip: 'Pick Image',
child: Icon(Icons.photo_library),
backgroundColor: Theme.of(context).primaryColor,
)
: Container(),
));
}
}