Last active
January 11, 2020 14:36
-
-
Save BoHellgren/a3317e7a507aa80bd9da70fca62aff71 to your computer and use it in GitHub Desktop.
Dog Camera step 1
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:flutter/services.dart'; | |
import 'package:camera/camera.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
CameraController _controller; | |
bool _cameraInitialized = false; | |
String _topText = ''; | |
@override | |
void initState() { | |
super.initState(); | |
SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]); | |
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark); | |
_initializeCamera(); | |
} | |
void _initializeCamera() async { | |
_topText = 'Waiting for camera initialization\n'; | |
List<CameraDescription> cameras = await availableCameras(); | |
_controller = CameraController(cameras[0], ResolutionPreset.medium); | |
_controller.initialize().then((_) { | |
_cameraInitialized = true; | |
_topText = 'Camera initialization complete\n'; | |
setState(() {}); | |
}); | |
} | |
@override | |
void dispose() { | |
_controller?.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, | |
body: Container( | |
color: Colors.black, | |
// constraints: const BoxConstraints.expand(), | |
child: Column( | |
children: <Widget>[ | |
Text( | |
_topText, | |
style: TextStyle(color: Colors.white, fontSize: 18), | |
), | |
_cameraInitialized | |
? Expanded( | |
child: OverflowBox( | |
maxWidth: double.infinity, | |
child: AspectRatio( | |
aspectRatio: _controller.value.aspectRatio, | |
child: CameraPreview(_controller)))) | |
: Container(), | |
], | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
backgroundColor: Colors.white, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment