camera_camera_2 2.0.1-dev0.1 camera_camera_2: ^2.0.1-dev0.1 copied to clipboard
The easy plugin camera for your project, platforms Android and iOS.
import 'dart:io';
import 'package:camera_camera_2/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, required 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),
),
);
}
}