Created
September 17, 2022 03:11
-
-
Save SuperPenguin/135fc013ec9803e02b068b89dfd669b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:camera/camera.dart'; | |
import 'package:flutter/material.dart'; | |
Future<void> main() async { | |
WidgetsFlutterBinding.ensureInitialized(); | |
runApp(const App()); | |
} | |
class App extends StatelessWidget { | |
const App({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
routes: { | |
'/': (context) => const HomeScreen(), | |
'/camera': (context) { | |
final camera = | |
ModalRoute.of(context)?.settings.arguments as CameraDescription; | |
return CameraScreen(camera: camera); | |
}, | |
}, | |
); | |
} | |
} | |
class HomeScreen extends StatefulWidget { | |
const HomeScreen({Key? key}) : super(key: key); | |
@override | |
State<HomeScreen> createState() => _HomeScreenState(); | |
} | |
class _HomeScreenState extends State<HomeScreen> { | |
late Future<List<CameraDescription>> _cameras; | |
@override | |
void initState() { | |
super.initState(); | |
_cameras = availableCameras(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Camera Demo'), | |
), | |
body: FutureBuilder<List<CameraDescription>>( | |
future: _cameras, | |
builder: (context, snapshot) { | |
switch (snapshot.connectionState) { | |
case ConnectionState.none: | |
case ConnectionState.waiting: | |
return const Center( | |
child: CircularProgressIndicator.adaptive(), | |
); | |
case ConnectionState.active: | |
case ConnectionState.done: | |
final error = snapshot.error; | |
if (error != null) { | |
return Center( | |
child: Padding( | |
padding: const EdgeInsets.all(24.0), | |
child: Text(error.toString()), | |
), | |
); | |
} | |
final data = snapshot.data as List<CameraDescription>; | |
if (data.isEmpty) { | |
return const Center( | |
child: Padding( | |
padding: EdgeInsets.all(24.0), | |
child: Text('This device does not have any camera'), | |
), | |
); | |
} | |
return ListView.builder( | |
itemCount: data.length, | |
itemBuilder: (context, index) { | |
final camera = data[index]; | |
return Card( | |
child: InkWell( | |
onTap: () { | |
Navigator.of(context).pushNamed( | |
'/camera', | |
arguments: camera, | |
); | |
}, | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Text('Name: ${camera.name}'), | |
Text('Direction: ${camera.lensDirection}'), | |
], | |
), | |
), | |
), | |
); | |
}, | |
); | |
} | |
}, | |
), | |
); | |
} | |
} | |
class CameraScreen extends StatefulWidget { | |
const CameraScreen({ | |
Key? key, | |
required this.camera, | |
}) : super(key: key); | |
final CameraDescription camera; | |
@override | |
State<CameraScreen> createState() => _CameraScreenState(); | |
} | |
class _CameraScreenState extends State<CameraScreen> { | |
late CameraController _cameraController; | |
late Future<void> _controllerInit; | |
@override | |
void initState() { | |
super.initState(); | |
_cameraController = CameraController(widget.camera, ResolutionPreset.max); | |
_controllerInit = _cameraController.initialize(); | |
} | |
@override | |
void didUpdateWidget(covariant CameraScreen oldWidget) { | |
super.didUpdateWidget(oldWidget); | |
if (oldWidget.camera != widget.camera) { | |
_cameraController.dispose(); | |
_cameraController = CameraController(widget.camera, ResolutionPreset.max); | |
_controllerInit = _cameraController.initialize(); | |
} | |
} | |
@override | |
void dispose() { | |
_cameraController.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Camera Preview'), | |
), | |
body: FutureBuilder<void>( | |
future: _controllerInit, | |
builder: (context, snapshot) { | |
switch (snapshot.connectionState) { | |
case ConnectionState.none: | |
case ConnectionState.waiting: | |
return const Center( | |
child: CircularProgressIndicator.adaptive(), | |
); | |
case ConnectionState.active: | |
case ConnectionState.done: | |
final error = snapshot.error; | |
if (error != null) { | |
return Center( | |
child: Padding( | |
padding: const EdgeInsets.all(24.0), | |
child: Text(error.toString()), | |
), | |
); | |
} | |
return CameraPreview(_cameraController); | |
} | |
}, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment