Created
March 22, 2022 12:32
-
-
Save daohoangson/7a0c36f22fab217de8000d60565fd870 to your computer and use it in GitHub Desktop.
Flutter CameraPreview in ClipRRect example
This file contains 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:flutter/material.dart'; | |
import 'package:camera/camera.dart'; | |
void main() => runApp(const CameraApp()); | |
class CameraApp extends StatefulWidget { | |
const CameraApp({Key? key}) : super(key: key); | |
@override | |
_CameraAppState createState() => _CameraAppState(); | |
} | |
class _CameraAppState extends State<CameraApp> { | |
List<CameraDescription>? _cameras; | |
CameraController? _controller; | |
int? _index; | |
var _isSwitching = false; | |
@override | |
void initState() { | |
super.initState(); | |
availableCameras().then((cameras) { | |
if (!mounted) return; | |
setState(() => _cameras = cameras); | |
}); | |
} | |
@override | |
void dispose() { | |
_controller?.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
final cameras = _cameras; | |
final controller = _controller; | |
return MaterialApp( | |
home: Scaffold( | |
body: Column( | |
children: [ | |
SafeArea( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: AspectRatio( | |
aspectRatio: 1, | |
child: LayoutBuilder( | |
builder: (_, constraints) { | |
final width = constraints.maxWidth; | |
assert(width.isFinite); | |
return ClipRRect( | |
borderRadius: BorderRadius.circular(width), | |
child: OverflowBox( | |
maxHeight: double.infinity, | |
maxWidth: double.infinity, | |
child: SizedBox( | |
width: width, | |
child: controller != null | |
? CameraPreview(controller) | |
: const DecoratedBox( | |
decoration: BoxDecoration( | |
color: Colors.grey, | |
), | |
), | |
), | |
), | |
); | |
}, | |
), | |
), | |
), | |
), | |
if (cameras != null) | |
...cameras.asMap().entries.map( | |
(entry) => Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: ElevatedButton( | |
onPressed: _isSwitching || entry.key == _index | |
? null | |
: () => _switchCamera(entry.key, entry.value), | |
child: Text('cameras[${entry.key}]'), | |
), | |
), | |
) | |
else | |
const Padding( | |
padding: EdgeInsets.all(8.0), | |
child: Text('Loading cameras...'), | |
) | |
], | |
), | |
), | |
); | |
} | |
void _switchCamera(int index, CameraDescription camera) async { | |
if (_isSwitching) return; | |
setState(() { | |
_controller = null; | |
_index = index; | |
_isSwitching = true; | |
}); | |
final controller = CameraController(camera, ResolutionPreset.max); | |
await controller.initialize(); | |
if (!mounted) { | |
controller.dispose(); | |
return; | |
} | |
setState(() { | |
_controller = controller; | |
_isSwitching = false; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment