Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:io';
- import 'package:image_picker/image_picker.dart';
- import 'package:flutter/material.dart';
- void main() {
- runApp(MyApp());
- }
- class MyApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: MyHomePage(),
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- File _image;
- final picker = ImagePicker();
- Future getImg() async {
- final pickedfile = await picker.getImage(source: ImageSource.camera);
- final File file = File(pickedfile.path);
- setState(() {
- if (pickedfile != null) {
- _image = file;
- } else {
- print('no image');
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(),
- body: Center(
- child: (_image == null) ? Text('no image') : Image.file(_image),
- ),
- floatingActionButton: FloatingActionButton(
- onPressed: () => getImg(),
- tooltip: 'pick image',
- child: Icon(Icons.add_a_photo),
- ), // This trailing comma makes auto-formatting nicer for build methods.
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement