Skip to content

Instantly share code, notes, and snippets.

@BoHellgren
Last active January 11, 2020 14:36
Show Gist options
  • Save BoHellgren/a3317e7a507aa80bd9da70fca62aff71 to your computer and use it in GitHub Desktop.
Save BoHellgren/a3317e7a507aa80bd9da70fca62aff71 to your computer and use it in GitHub Desktop.
Dog Camera step 1
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