Camera and Gesture (Error)
Main.dart
import 'package:flutter/material.dart';
import 'MyHomePage.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Camera and Gesture',
theme: ThemeData(primaryColor: Colors.red),
home: MyHomePage(),
);
}
}
HomePage.dart
Error on // File _image;
import 'dart:html';
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:io';
import 'package:image_picker/image_picker.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File _image;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('My Camera App'),
),
body: Center(
child: _image == null ? Text('No Image') : Image.file(_image),
),
floatingActionButton: FloatingActionButton(onPressed: _optionsDialogBox, child: Icon(Icons.add_a_photo), tooltip: 'Open Camera'),
);
}
Future<void> _optionsDialogBox() {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: Colors.orange,
shape: StadiumBorder(),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
GestureDetector(
child: Text(
'Take a Picture',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
onTap: openCamera,
),
Padding(
padding: EdgeInsets.all(10.0),
),
GestureDetector(
child: Text(
'Select an Image from Gallery',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
),
onTap: openGallery,
),
],
),
),
);
});
}
Future openCamera() async {
var image = await ImagePicker().pickImage(source: ImageSource.camera);
setState(() {
_image = image;
});
}
Future openGallery() async {
var picture = await ImagePicker().pickImage(source: ImageSource.gallery);
setState(() {
_image = picture;
});
}
}
iosPermission
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app requires access to the microphone</string>
<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera.</string>
Comments
Post a Comment