Flutter 在图库视图网格中渲染应用程序中拍摄的照片
我正在创建一个应用程序,当没有用户生成的内容时,它会显示一条消息,但当用户拍照时,它会在网格画廊视图中显示照片 我已经在使用条件逻辑来实现这一点,但是,当我按下相机按钮时,相机会显示并拍摄照片,但是照片不会以所需的格式返回主页 下面有一个关于我试图实现的流程的屏幕截图,供您参考。希望有人能帮忙纠正 我有下面完整的生殖代码,可以复制正在发生的事情。基于我所拥有的、我的研究和知识,这应该已经实现了,因为我正在通过相机页面中的以下内容将拍摄的照片推回到主页Flutter 在图库视图网格中渲染应用程序中拍摄的照片,flutter,Flutter,我正在创建一个应用程序,当没有用户生成的内容时,它会显示一条消息,但当用户拍照时,它会在网格画廊视图中显示照片 我已经在使用条件逻辑来实现这一点,但是,当我按下相机按钮时,相机会显示并拍摄照片,但是照片不会以所需的格式返回主页 下面有一个关于我试图实现的流程的屏幕截图,供您参考。希望有人能帮忙纠正 我有下面完整的生殖代码,可以复制正在发生的事情。基于我所拥有的、我的研究和知识,这应该已经实现了,因为我正在通过相机页面中的以下内容将拍摄的照片推回到主页 Navigator.push(
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Homepage_1(
imgPath: path,
)),
这是相机页面的代码
import 'package:camera/camera.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:image_picker/image_picker.dart';
import 'preview_screen.dart';
class Camera extends StatefulWidget {
@override
_CameraScreenState createState() => _CameraScreenState();
}
class _CameraScreenState extends State {
CameraController controller;
List cameras;
int selectedCameraIndex;
String imgPath;
var image; //
List imageArray = []; //
_openGallery() async {
image = await ImagePicker.pickImage(source: ImageSource.camera);
imageArray.add(image);
setState(() {
imageArray;
});
}
@override
void initState() {
super.initState();
availableCameras().then((availableCameras) {
cameras = availableCameras;
if (cameras.length > 0) {
setState(() {
selectedCameraIndex = 0;
});
_initCameraController(cameras[selectedCameraIndex]).then((void v) {});
} else {
print('No camera available');
}
}).catchError((err) {
print('Error :${err.code}Error message : ${err.message}');
});
}
Future _initCameraController(CameraDescription cameraDescription) async {
if (controller != null) {
await controller.dispose();
}
controller = CameraController(cameraDescription, ResolutionPreset.high);
controller.addListener(() {
if (mounted) {
setState(() {});
}
if (controller.value.hasError) {
print('Camera error ${controller.value.errorDescription}');
}
});
try {
await controller.initialize();
} on CameraException catch (e) {
_showCameraException(e);
}
if (mounted) {
setState(() {});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 1,
child: _cameraPreviewWidget(),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 120,
width: double.infinity,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
_cameraToggleRowWidget(),
_cameraControlWidget(context),
Spacer()
],
),
),
)
],
),
),
),
);
}
/// Display Camera preview.
Widget _cameraPreviewWidget() {
if (controller == null || !controller.value.isInitialized) {
return const Text(
'Loading',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.w900,
),
);
}
return AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: CameraPreview(controller),
);
}
/// Display the control bar with buttons to take pictures
Widget _cameraControlWidget(context) {
return Expanded(
child: Align(
alignment: Alignment.center,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
FloatingActionButton(
child: Icon(
Icons.camera,
color: Colors.black,
),
backgroundColor: Colors.white,
onPressed: () {
_openGallery();
},
)
],
),
),
);
}
/// Display a row of toggle to select the camera (or a message if no camera is available).
Widget _cameraToggleRowWidget() {
if (cameras == null || cameras.isEmpty) {
return Spacer();
}
CameraDescription selectedCamera = cameras[selectedCameraIndex];
CameraLensDirection lensDirection = selectedCamera.lensDirection;
return Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: FlatButton.icon(
onPressed: _onSwitchCamera,
icon: Icon(
_getCameraLensIcon(lensDirection),
color: Colors.white,
size: 24,
),
label: Text(
'${lensDirection.toString().substring(lensDirection.toString().indexOf('.') + 1).toUpperCase()}',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500),
),
),
),
);
}
IconData _getCameraLensIcon(CameraLensDirection direction) {
switch (direction) {
case CameraLensDirection.back:
return CupertinoIcons.switch_camera;
case CameraLensDirection.front:
return CupertinoIcons.switch_camera_solid;
case CameraLensDirection.external:
return Icons.camera;
default:
return Icons.device_unknown;
}
}
void _showCameraException(CameraException e) {
String errorText = 'Error:${e.code}\nError message : ${e.description}';
print(errorText);
}
void _onCapturePressed(context) async {
try {
final path =
join((await getTemporaryDirectory()).path, '${DateTime.now()}.png');
await controller.takePicture();
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Homepage_1(
imgPath: path,
)),
);
} catch (e) {
_showCameraException(e);
}
}
void _onSwitchCamera() {
selectedCameraIndex =
selectedCameraIndex < cameras.length - 1 ? selectedCameraIndex + 1 : 0;
CameraDescription selectedCamera = cameras[selectedCameraIndex];
_initCameraController(selectedCamera);
}
}
下面是包含条件逻辑的代码本身
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class Homepage_1 extends StatefulWidget {
//specify a FINAL variable
final imgPath;
//this is the constructor which allows you to send it
const Homepage_1({this.imgPath});
@override
_Homepage_1State createState() => _Homepage_1State();
}
class _Homepage_1State extends State<Homepage_1> {
var image;
List imageArray = [];
_openGallery() async {
image = await ImagePicker.pickImage(source: ImageSource.camera);
imageArray.add(image);
setState(() {
imageArray;
});
}
@override
Widget build(BuildContext context) {
print(imageArray);
return Scaffold(
resizeToAvoidBottomInset: false,
resizeToAvoidBottomPadding: false,
backgroundColor: Colors.white,
body: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Padding(
padding:
const EdgeInsets.only(top: 100, left: 40, right: 0, bottom: 0),
child:
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(
'Title',
style: TextStyle(
fontSize: 60,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Container(
margin:
EdgeInsets.only(top: 0, left: 0, right: 70, bottom: 0),
child: imageArray.isEmpty
? Column(children: [
Text(
'Yikes! You have no photos',
style: TextStyle(
fontSize: 19,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Text(
'Click the circular button below.',
style: TextStyle(
fontSize: 15,
fontFamily: 'Avenir',
fontWeight: FontWeight.w500,
),
),
])
: GridView.count(
crossAxisCount: 2,
children: List.generate(imageArray.length, (index) {
var img = imageArray[index];
return Container(child: Image.file(this.widget.imgPath));
})))
]),
)
]));
}
}
这里也是我的底部导航,供您参考
import 'package:flutter/material.dart';
import 'Camera.dart';
import 'Homepage_1.dart';
import 'Settings.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
// properties
int currentTab = 0;
final List<Widget> screens = [
Settings(),
Camera(),
Homepage_1(),
];
Widget currentScreen = Homepage_1();
final PageStorageBucket bucket = PageStorageBucket();
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageStorage(
child: currentScreen,
bucket: bucket,
),
floatingActionButton: ConstrainedBox(
constraints: BoxConstraints.tightFor(width: 80, height: 80),
child: ElevatedButton(
child: Icon(Icons.center_focus_strong, size: 39),
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Camera()));
},
style: ElevatedButton.styleFrom(
shape: CircleBorder(), primary: Color(0xff33333D)),
),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
child: Container(
height: 60,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(children: <Widget>[
MaterialButton(
minWidth: 150,
onPressed: () {
setState(
() {
currentScreen = Homepage_1();
currentTab = 0;
},
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ImageIcon(
AssetImage("assets/Home_Selected.png"),
color: currentTab == 0
? Color(0xff33333D)
: Color(0xffC6CBD1),
),
]),
)
]),
Row(children: <Widget>[
MaterialButton(
minWidth: 150,
onPressed: () {
setState(
() {
currentScreen = Settings();
currentTab = 1;
},
);
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ImageIcon(
AssetImage("assets/Settings.png"),
color: currentTab == 1
? Color(0xff33333D)
: Color(0xffC6CBD1),
),
]),
)
])
]),
)),
);
}
}
您没有以正确的方式将其发送回主页。主页_1小部件中没有imgPath变量,您必须在以下步骤之前添加它:
class Homepage_1 extends StatefulWidget {
//specify a FINAL variable
final imgPath;
//this is the constructor which allows you to send it
const Homepage_1({this.imgPath});
@override
_Homepage_1State createState() => _Homepage_1State();
}
现在,您可以访问State类中的imgPath变量,如下所示:
this.widget.imgPath
按如下方式推至主屏幕_1:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Homepage_1(
imgPath: path,
)),
您没有以正确的方式将其发送回主页。主页_1小部件中没有imgPath变量,您必须在以下步骤之前添加它:
class Homepage_1 extends StatefulWidget {
//specify a FINAL variable
final imgPath;
//this is the constructor which allows you to send it
const Homepage_1({this.imgPath});
@override
_Homepage_1State createState() => _Homepage_1State();
}
现在,您可以访问State类中的imgPath变量,如下所示:
this.widget.imgPath
按如下方式推至主屏幕_1:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Homepage_1(
imgPath: path,
)),
我假设您仍在测试camera库,并尝试将图像从类camera传递到类Homepage_1,所以我只关注数据传递部分。您犯了一些小错误: 类摄像机 我假设您想使用_onCapturePressednot _openGallery将图像路径发送到主页。实际文件路径是从takePicture返回的
final xFile = await controller.takePicture();
...
Navigator.push(
...
imgPath: xFile.path,
...
班级主页(1)
使用imageArray构建网格,但不向其中添加任何文件/路径
@override
Widget build(BuildContext context) {
if (widget.imgPath != null) {
imageArray.add(File(widget.imgPath));
}
...
child: GridView.count(
crossAxisCount: 2,
children: List.generate(
imageArray.length,
(index) {
var img = imageArray[index];
return Container(child: Image.file(img));
},
),
),
那么它现在应该可以工作了
注意:
Library ImagePicker已打开手机上的本机相机/多媒体资料,Library camera用于在应用程序中自定义您的相机,不建议同时使用它们。
因为类摄像头和类主页_1不在使用Navigator.push的屏幕的同一级别上,所以可以将摄像头屏幕堆叠在主页_1上。更新主页_1的最佳方法是在相机中使用Navigator.pop并将图像路径带回主页_1。你可以查看参考资料
我假设您仍在测试camera库,并尝试将图像从类camera传递到类Homepage_1,所以我只关注数据传递部分。您犯了一些小错误: 类摄像机 我假设您想使用_onCapturePressednot _openGallery将图像路径发送到主页。实际文件路径是从takePicture返回的
final xFile = await controller.takePicture();
...
Navigator.push(
...
imgPath: xFile.path,
...
班级主页(1)
使用imageArray构建网格,但不向其中添加任何文件/路径
@override
Widget build(BuildContext context) {
if (widget.imgPath != null) {
imageArray.add(File(widget.imgPath));
}
...
child: GridView.count(
crossAxisCount: 2,
children: List.generate(
imageArray.length,
(index) {
var img = imageArray[index];
return Container(child: Image.file(img));
},
),
),
那么它现在应该可以工作了
注意:
Library ImagePicker已打开手机上的本机相机/多媒体资料,Library camera用于在应用程序中自定义您的相机,不建议同时使用它们。
因为类摄像头和类主页_1不在使用Navigator.push的屏幕的同一级别上,所以可以将摄像头屏幕堆叠在主页_1上。更新主页_1的最佳方法是在相机中使用Navigator.pop并将图像路径带回主页_1。你可以查看参考资料
我对你的文件做了一些更改。请调查一下。虽然您可以直接在主页中使用ImagePicker.pickImage,但我按照您的代码解决了它。 我使用了回调函数将camera.dart中的图像输入到您的主页。有关回调函数的更多了解,请参考以下链接 然后是相机文件
import 'package:camera/camera.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class Camera extends StatefulWidget {
Function setData;
Camera({Key key,this.setData}):super(key: key);
@override
_CameraScreenState createState() => _CameraScreenState();
}
class _CameraScreenState extends State<Camera> {
CameraController controller;
List cameras;
int selectedCameraIndex;
String imgPath;
var image;
Future _openGallery() async {
image = await ImagePicker.pickImage(source: ImageSource.camera);
if(widget.setData!=null) {
widget.setData(image);
}
}
@override
void initState() {
super.initState();
availableCameras().then((availableCameras) {
cameras = availableCameras;
if (cameras.length > 0) {
setState(() {
selectedCameraIndex = 0;
});
_initCameraController(cameras[selectedCameraIndex]).then((void v) {});
} else {
print('No camera available');
}
}).catchError((err) {
print('Error :${err.code}Error message : ${err.message}');
});
}
Future _initCameraController(CameraDescription cameraDescription) async {
if (controller != null) {
await controller.dispose();
}
controller = CameraController(cameraDescription, ResolutionPreset.high);
controller.addListener(() {
if (mounted) {
setState(() {});
}
if (controller.value.hasError) {
print('Camera error ${controller.value.errorDescription}');
}
});
try {
await controller.initialize();
} on CameraException catch (e) {
_showCameraException(e);
}
if (mounted) {
setState(() {});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 1,
child: _cameraPreviewWidget(),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 120,
width: double.infinity,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
_cameraToggleRowWidget(),
_cameraControlWidget(context),
Spacer()
],
),
),
)
],
),
),
),
);
}
/// Display Camera preview.
Widget _cameraPreviewWidget() {
if (controller == null || !controller.value.isInitialized) {
return const Text(
'Loading',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.w900,
),
);
}
return AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: CameraPreview(controller),
);
}
/// Display the control bar with buttons to take pictures
Widget _cameraControlWidget(context) {
return Expanded(
child: Align(
alignment: Alignment.center,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
FloatingActionButton(
child: Icon(
Icons.camera,
color: Colors.black,
),
backgroundColor: Colors.white,
onPressed: () {
// getImage();
_openGallery();
Navigator.pop(context);
},
)
],
),
),
);
}
/// Display a row of toggle to select the camera (or a message if no camera is available).
Widget _cameraToggleRowWidget() {
if (cameras == null || cameras.isEmpty) {
return Spacer();
}
CameraDescription selectedCamera = cameras[selectedCameraIndex];
CameraLensDirection lensDirection = selectedCamera.lensDirection;
return Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: FlatButton.icon(
onPressed: _onSwitchCamera,
icon: Icon(
_getCameraLensIcon(lensDirection),
color: Colors.white,
size: 24,
),
label: Text(
'${lensDirection.toString().substring(lensDirection.toString().indexOf('.') + 1).toUpperCase()}',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500),
),
),
),
);
}
IconData _getCameraLensIcon(CameraLensDirection direction) {
switch (direction) {
case CameraLensDirection.back:
return CupertinoIcons.switch_camera;
case CameraLensDirection.front:
return CupertinoIcons.switch_camera_solid;
case CameraLensDirection.external:
return Icons.camera;
default:
return Icons.device_unknown;
}
}
void _showCameraException(CameraException e) {
String errorText = 'Error:${e.code}\nError message : ${e.description}';
print(errorText);
}
void _onSwitchCamera() {
selectedCameraIndex =
selectedCameraIndex < cameras.length - 1 ? selectedCameraIndex + 1 : 0;
CameraDescription selectedCamera = cameras[selectedCameraIndex];
_initCameraController(selectedCamera);
}
}
主页_1显示图像
import 'dart:io';
import 'package:flutter/material.dart';
class Homepage_1 extends StatefulWidget {
final List<File> imageArray;
Homepage_1({Key key, this.imageArray}) : super(key: key);
@override
_Homepage_1State createState() => _Homepage_1State();
}
class _Homepage_1State extends State<Homepage_1> {
var image;
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
resizeToAvoidBottomPadding: false,
backgroundColor: Colors.white,
body: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Padding(
padding:
const EdgeInsets.only(top: 100, left: 40, right: 0, bottom: 0),
child:
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(
'Title',
style: TextStyle(
fontSize: 60,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Container(
margin:
EdgeInsets.only(top: 0, left: 0, right: 70, bottom: 0),
child: widget.imageArray.isEmpty
? Column(children: [
Text(
'Yikes! You have no photos',
style: TextStyle(
fontSize: 19,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Text(
'Click the circular button below.',
style: TextStyle(
fontSize: 15,
fontFamily: 'Avenir',
fontWeight: FontWeight.w500,
),
),
])
:
GridView.count(
shrinkWrap: true,
crossAxisCount: 2,
childAspectRatio: 1.0,
padding: const EdgeInsets.all(4.0),
children:
List.generate(widget.imageArray.length, (index) {
return Container(
child: Image.file(widget.imageArray[index]));
})))
]),
)
]));
}
}
我希望这能解决你的问题,因为我为此花了很多时间。享受:我对你的文件做了一些更改。请调查一下。虽然您可以直接在主页中使用ImagePicker.pickImage,但我按照您的代码解决了它。 我使用了回调函数将camera.dart中的图像输入到您的主页。有关回调函数的更多了解,请参考以下链接 然后是相机文件
import 'package:camera/camera.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class Camera extends StatefulWidget {
Function setData;
Camera({Key key,this.setData}):super(key: key);
@override
_CameraScreenState createState() => _CameraScreenState();
}
class _CameraScreenState extends State<Camera> {
CameraController controller;
List cameras;
int selectedCameraIndex;
String imgPath;
var image;
Future _openGallery() async {
image = await ImagePicker.pickImage(source: ImageSource.camera);
if(widget.setData!=null) {
widget.setData(image);
}
}
@override
void initState() {
super.initState();
availableCameras().then((availableCameras) {
cameras = availableCameras;
if (cameras.length > 0) {
setState(() {
selectedCameraIndex = 0;
});
_initCameraController(cameras[selectedCameraIndex]).then((void v) {});
} else {
print('No camera available');
}
}).catchError((err) {
print('Error :${err.code}Error message : ${err.message}');
});
}
Future _initCameraController(CameraDescription cameraDescription) async {
if (controller != null) {
await controller.dispose();
}
controller = CameraController(cameraDescription, ResolutionPreset.high);
controller.addListener(() {
if (mounted) {
setState(() {});
}
if (controller.value.hasError) {
print('Camera error ${controller.value.errorDescription}');
}
});
try {
await controller.initialize();
} on CameraException catch (e) {
_showCameraException(e);
}
if (mounted) {
setState(() {});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 1,
child: _cameraPreviewWidget(),
),
Align(
alignment: Alignment.bottomCenter,
child: Container(
height: 120,
width: double.infinity,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
_cameraToggleRowWidget(),
_cameraControlWidget(context),
Spacer()
],
),
),
)
],
),
),
),
);
}
/// Display Camera preview.
Widget _cameraPreviewWidget() {
if (controller == null || !controller.value.isInitialized) {
return const Text(
'Loading',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
fontWeight: FontWeight.w900,
),
);
}
return AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: CameraPreview(controller),
);
}
/// Display the control bar with buttons to take pictures
Widget _cameraControlWidget(context) {
return Expanded(
child: Align(
alignment: Alignment.center,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
FloatingActionButton(
child: Icon(
Icons.camera,
color: Colors.black,
),
backgroundColor: Colors.white,
onPressed: () {
// getImage();
_openGallery();
Navigator.pop(context);
},
)
],
),
),
);
}
/// Display a row of toggle to select the camera (or a message if no camera is available).
Widget _cameraToggleRowWidget() {
if (cameras == null || cameras.isEmpty) {
return Spacer();
}
CameraDescription selectedCamera = cameras[selectedCameraIndex];
CameraLensDirection lensDirection = selectedCamera.lensDirection;
return Expanded(
child: Align(
alignment: Alignment.centerLeft,
child: FlatButton.icon(
onPressed: _onSwitchCamera,
icon: Icon(
_getCameraLensIcon(lensDirection),
color: Colors.white,
size: 24,
),
label: Text(
'${lensDirection.toString().substring(lensDirection.toString().indexOf('.') + 1).toUpperCase()}',
style: TextStyle(color: Colors.white, fontWeight: FontWeight.w500),
),
),
),
);
}
IconData _getCameraLensIcon(CameraLensDirection direction) {
switch (direction) {
case CameraLensDirection.back:
return CupertinoIcons.switch_camera;
case CameraLensDirection.front:
return CupertinoIcons.switch_camera_solid;
case CameraLensDirection.external:
return Icons.camera;
default:
return Icons.device_unknown;
}
}
void _showCameraException(CameraException e) {
String errorText = 'Error:${e.code}\nError message : ${e.description}';
print(errorText);
}
void _onSwitchCamera() {
selectedCameraIndex =
selectedCameraIndex < cameras.length - 1 ? selectedCameraIndex + 1 : 0;
CameraDescription selectedCamera = cameras[selectedCameraIndex];
_initCameraController(selectedCamera);
}
}
主页_1显示图像
import 'dart:io';
import 'package:flutter/material.dart';
class Homepage_1 extends StatefulWidget {
final List<File> imageArray;
Homepage_1({Key key, this.imageArray}) : super(key: key);
@override
_Homepage_1State createState() => _Homepage_1State();
}
class _Homepage_1State extends State<Homepage_1> {
var image;
@override
void initState() {
// TODO: implement initState
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
resizeToAvoidBottomPadding: false,
backgroundColor: Colors.white,
body: Column(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
Padding(
padding:
const EdgeInsets.only(top: 100, left: 40, right: 0, bottom: 0),
child:
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
Text(
'Title',
style: TextStyle(
fontSize: 60,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Container(
margin:
EdgeInsets.only(top: 0, left: 0, right: 70, bottom: 0),
child: widget.imageArray.isEmpty
? Column(children: [
Text(
'Yikes! You have no photos',
style: TextStyle(
fontSize: 19,
fontFamily: 'Avenir',
fontWeight: FontWeight.w900,
),
),
Text(
'Click the circular button below.',
style: TextStyle(
fontSize: 15,
fontFamily: 'Avenir',
fontWeight: FontWeight.w500,
),
),
])
:
GridView.count(
shrinkWrap: true,
crossAxisCount: 2,
childAspectRatio: 1.0,
padding: const EdgeInsets.all(4.0),
children:
List.generate(widget.imageArray.length, (index) {
return Container(
child: Image.file(widget.imageArray[index]));
})))
]),
)
]));
}
}
我希望这能解决你的问题,因为我为此花了很多时间。享受:刚刚尝试过,但仍然没有任何内容呈现到主页上。我编辑了代码以包含您的编辑。。。你能再看看吗?在这里不知所措…只是尝试了一下,但仍然没有任何东西呈现到主页上。我编辑了代码以包含您的编辑。。。你能再看看吗?在这里不知所措…只是一个建议,尽量保持正确的命名惯例。从您要将图像发送到何处以及图像应显示在何处,这让人感到困惑。建议您尽量保持正确的命名约定。从何而来令人困惑 e您要发送图像,以及图像应显示的位置。