:::

6-14 加入拍照功能

  1. 先安裝 https://pub.dev/packages/image_picker
  2. 修改 \photo_app\android\app\src\main\AndroidManifest.xml
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="net.tad0616.photo_app">
       <application
            android:label="photo_app"
            android:requestLegacyExternalStorage="true"
            android:icon="@mipmap/ic_launcher">
            ...略...
        </application>
    
        <queries>
            <intent>
                <action android:name="android.media.action.IMAGE_CAPTURE" />
            </intent>
        </queries>
    </manifest>
    

     

  3. 修改 \photo_app\ios\Runner\Info.plist
    ...略...
    	<key>LSRequiresIPhoneOS</key>
    	<true/>
    
    	<key>NSCameraUsageDescription</key>
    	<string>Used to demonstrate image picker plugin</string>
    	<key>NSMicrophoneUsageDescription</key>
    	<string>Used to capture audio for image picker plugin</string>
    	<key>NSPhotoLibraryUsageDescription</key>
    	<string>Used to demonstrate image picker plugin</string>
    	<key>UIBackgroundModes</key>
    	<array>
    	    <string>remote-notification</string>
    	</array>
    
    	<key>UILaunchStoryboardName</key>
    ...略...

     

  4. 範例:
    import 'package:flutter/material.dart';
    import 'dart:io';
    import 'package:image_picker/image_picker.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: '拍照',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: PhotoPage(title: '拍照App'),
        );
      }
    }
    
    class PhotoPage extends StatefulWidget {
      PhotoPage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _PhotoPageState createState() => _PhotoPageState();
    }
    
    class _PhotoPageState extends State<PhotoPage> {
      File _image;
      final picker = ImagePicker();
    
      Future getImage() async {
        final pickedFile = await picker.getImage(source: ImageSource.camera);
    
        setState(() {
          if (pickedFile != null) {
            _image = File(pickedFile.path);
          } else {
            print('No image selected.');
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  '相片列表',
                  style: Theme.of(context).textTheme.headline4,
                ),
                _image == null
                    ? Text('No image selected.')
                    : Image.file(_image),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: getImage,
            tooltip: 'Pick Image',
            child: Icon(Icons.add_a_photo),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }
    

     

  5.  

:::

書籍目錄

展開 | 闔起

https%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D42%26tbdsn%3D1905

計數器

今天: 1044104410441044
昨天: 3438343834383438
總計: 7391373739137373913737391373739137373913737391373