camera_camera 2.0.0-nullsafety camera_camera: ^2.0.0-nullsafety copied to clipboard
The easy plugin camera for your project, platforms Android and iOS.
import 'dart:io';
import 'package:camera_camera/camera_camera.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Camera Camera 2.0',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Camera Camera 2.0'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final photos = <File>[];
void openCamera() {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => CameraCamera(
onFile: (file) {
photos.add(file);
Navigator.pop(context);
setState(() {});
},
)));
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: Text(widget.title),
),
body: GridView.builder(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemCount: photos.length,
itemBuilder: (_, index) => Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: size.width,
child: Image.file(
photos[index],
fit: BoxFit.cover,
),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: openCamera,
child: Icon(Icons.camera_alt),
),
);
}
}