0

我有一个简单的应用程序,可以从图库中选择图像。当我将integration_test包添加到应用程序时,它在调试模式下工作正常,但是当我打开图像选择器时构建崩溃。

这是实现:

camera_widget.dart

import 'package:flutter/material.dart';
import 'package:multi_image_picker/multi_image_picker.dart';
import 'package:permission_handler/permission_handler.dart';

class CameraWidget extends StatefulWidget {
  @override
  _CameraWidgetState createState() => _CameraWidgetState();
}

class _CameraWidgetState extends State<CameraWidget> {
  List<Asset> images = <Asset>[];
  String _error = 'No error found';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  Widget buildGridView(){
    return Scaffold(
      body: new GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(new FocusNode());
        },
        child: GridView.count(crossAxisCount: 4,
          children: List.generate(images.length, (index) {
            Asset asset = images[index];
            return AssetThumb(
              asset: asset,
              width: 300,
              height: 300,
            );
          }),
        ),
      ),
    );
  }

  Future<void> loadAssets() async{
    List<Asset> resultList = <Asset>[];
    String error = 'No error found';

    try{
      if(await checkAndRequestCameraPermissions()){
        resultList = await MultiImagePicker.pickImages(
          maxImages: 3,
          enableCamera: true,
          selectedAssets: images,
          cupertinoOptions: CupertinoOptions(takePhotoIcon: "chat"),
          materialOptions: MaterialOptions(
              actionBarColor: "#abcdef",
              actionBarTitle: "Example",
              allViewTitle: "All photos",
              useDetailsView: false,
              selectCircleStrokeColor: "#000000"
          ),
        );
      }

    } on Exception catch(e){
      error = e.toString();
      print(error);
    }

    if (!mounted) return;

    setState(() {
      images = resultList;
      _error = error;
    });
  }

  Future<bool> checkAndRequestCameraPermissions() async{
    PermissionStatus permissionStatus = await Permission.camera.status;
    if(permissionStatus != PermissionStatus.granted){
      Map<Permission, PermissionStatus> permissions = await [
        Permission.camera
      ].request();
      return permissions[Permission.camera] == PermissionStatus.granted;
    }
    else
      return true;
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Container(
            child: Column(
                children: [
                  OutlineButton(
                    child: Text('Choose image'),
                    onPressed: loadAssets,
                  ),
                  Flexible(
                    child: buildGridView(),
                  ),
                ]
            ),
          ),
        ),
      ),
    );
  }
}

发布规范.yaml

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  rflutter_alert: ^1.1.0
  multi_image_picker: ^4.7.15
  permission_handler: ^5.1.0+2
  integration_test: ^1.0.2+2

编辑:删除 integration_test 包后,该应用程序在调试和构建中都可以正常工作。

4

0 回答 0