импорт 'dart: async';
import 'dart: io';
import 'package: flutter / material.dart';
import 'package: camera / camera.dart';
import 'package: path / path.dart';
import 'package: path_provider / path_provider.dart';
класс CustomCamera расширяет StatefulWidget {
@ переопределение
_CameraAppState createState () => _CustomCameraState ();
}
class _CustomCameraState расширяет State {
Список камер;
контроллер CameraController;
bool _isReady = false;
bool переключен = ложь;
@ переопределение
void initState () {
super.initState();
_setupCameras(switched);
}
Future _setupCameras (bool Switched) async {
try {
// initialize cameras.
cameras = await availableCameras();
// initialize camera controllers.
controller = new CameraController(cameras[0], ResolutionPreset.medium);
await controller.initialize();
} on CameraException catch (_) {
// do something on error.
}
if (!mounted) return;
setState(() {
_isReady = true;
});
}
String newImage;
@ переопределение
Сборка виджета (контекст BuildContext) {
if (!_isReady) return Container();
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Stack(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: AspectRatio(
aspectRatio: controller.value.aspectRatio,
child: CameraPreview(controller)
),
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 26.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
InkWell(
child: Container(
height: 80.0,
width: 60.0,
color: Colors.black,
child: newImage == null ?
Image.asset ('assets / icons / city.png', подходит: BoxFit.cover,):
Image.file (File ( "$ newImage"))
),
onTap: (){
},
),
Opacity(
opacity: 0.9,
child: Image.asset('assets/buttons/videoButton.png',scale:
12,5)
),
InkWell(
child: Image.asset('assets/buttons/TakePhotoButton.png',scale: 4.5),
onTap: () async{
try {
await controller.initialize();
// Construct the path where the image should be saved using the path
// package.
final path = join(
// In this example, store the picture in the temp directory. Find
// the temp directory using the `path_provider` plugin.
(await getApplicationDocumentsDirectory()).path,
'hello.png',
);
//Set the variable
setState(() {
newImage = path;
});
var echo = path.toString();
// Attempt to take a picture and log where it's been saved
await controller.takePicture(path);
print("$echo this is the path");
} catch (e) {
// If an error occurs, log the error to the console.
print(e);
}
},
),
Opacity(
opacity: 0.9,
child: InkWell(
child: Image.asset('assets/buttons/flipCamera.png',scale: 14.5),
onTap: (){
setState((){
switched = true;
});
},
)
),
Opacity(
opacity: 1.0,
child: Image.asset('assets/buttons/Livebutton.png',scale: 14.5)
),
],
),
),
),
Align(
alignment: Alignment.topRight,
child: Padding(
padding: const EdgeInsets.fromLTRB(0.0, 50.0, 26.0, 0.0),
child: InkWell(
child: Icon(
Icons.arrow_forward_ios,
color: Colors.white,
size: 30.0,
),
onTap: () => Navigator.pop(context)
),
),
)
],
),
),
);
}
}