简体   繁体   English

如何在我的 main.dart 文件中使用 showChoiceDialog()

[英]how can I use showChoiceDialog() in my main.dart file

here is code of imagepicker in flutter .这是 flutter 中 imagepicker 的代码。

how can I use showChoiceDialog(), opengallery() ,openCamera() in my other files .如何在我的其他文件中使用showChoiceDialog()、opengallery()、openCamera() what class objects and variable should i have to make in order to use this functions in other file .how can I use showChoiceDialog(), opengallery() ,openCamera() in my other files .为了在其他文件中使用此函数,我应该制作什么类对象和变量。如何在我的其他文件中使用showChoiceDialog()、opengallery()、openCamera() what class objects and variable should i have to make in order to use this functions in other file .为了在其他文件中使用这个函数,我应该制作什么类对象和变量。

how can i set new image to Image Widgets .how can update the state.如何将新图像设置为图像小部件。如何更新状态。

imagepicker.dart图像选择器.dart

import 'dart:ffi';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class CameraWidget extends StatefulWidget {
  @override
  State createState() {
    // TODO: implement createState
    return CameraWidgetState();
  }
}

class CameraWidgetState extends State {
  PickedFile? imageFile = null;
  Future<void> showChoiceDialog(BuildContext context) {
    return showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text(
            "Choose option",
            style: TextStyle(color: Colors.blue),
          ),
          content: SingleChildScrollView(
            child: ListBody(
              children: [
                Divider(
                  height: 1,
                  color: Colors.blue,
                ),
                ListTile(
                  onTap: () {
                    _openGallery(context);
                  },
                  title: Text("Gallery"),
                  leading: Icon(
                    Icons.account_box,
                    color: Colors.blue,
                  ),
                ),
                Divider(
                  height: 1,
                  color: Colors.blue,
                ),
                ListTile(
                  onTap: () {
                    _openCamera(context);
                  },
                  title: Text("Camera"),
                  leading: Icon(
                    Icons.camera,
                    color: Colors.blue,
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("Pick Image Camera"),
        backgroundColor: Colors.green,
      ),
      body: Center(
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: [
              Card(
                child: (imageFile == null)
                    ? Text("Choose Image")
                    : Image.file(File(imageFile!.path)),
              ),
              Image.asset('images/img1.jpg'),
              MaterialButton(
                textColor: Colors.white,
                color: Colors.pink,
                onPressed: () {
                  showChoiceDialog(context);
                },
                child: Text("Select Image"),
              )
            ],
          ),
        ),
      ),
    );
  }

  void _openGallery(BuildContext context) async {
    final pickedFile = await ImagePicker().getImage(
      source: ImageSource.gallery,
    );
    setState(() {
      imageFile = pickedFile!;
    });

    Navigator.pop(context);
  }

  void _openCamera(BuildContext context) async {
    final pickedFile = await ImagePicker().getImage(
      source: ImageSource.camera,
    );
    setState(() {
      imageFile = pickedFile!;
    });
    Navigator.pop(context);
  }
}

You cannot access their properties as they are marked as private.您无法访问它们的属性,因为它们被标记为私有。 To mark a function/method as private, you put an underscore before its name.要将函数/方法标记为私有,请在其名称前添加下划线。 For example, checkUpdates() is a public method, and _checkUpdates() is a public method.例如, checkUpdates()是公共方法, _checkUpdates()是公共方法。

I would recommend putting these in another file if you plan on reusing them.如果您打算重用它们,我建议将它们放在另一个文件中。

A very basic method of doing this is to create a new file, image_service.dart一个非常基本的方法是创建一个新文件image_service.dart

Make a new class创建一个新班级

class ImageService {

 static void openGallery(BuildContext context) async {
    final pickedFile = await ImagePicker().getImage(
      source: ImageSource.gallery,
    );
    setState(() {
      imageFile = pickedFile!;
    });

    Navigator.pop(context);
  }

  static void openCamera(BuildContext context) async {
    final pickedFile = await ImagePicker().getImage(
      source: ImageSource.camera,
    );
    setState(() {
      imageFile = pickedFile!;
    });
    Navigator.pop(context);
  }
}

And then you can use it as follows.然后您可以按如下方式使用它。 ImageService.openGallery(context);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的 JS 文件中使用 main.dart 变量? - How can I use main.dart variable in my JS file? 如何将 If 语句添加到 main.dart 文件 - How Add a If statement to main.dart file 在 flutter 中,需要在我的 main.dart 文件中使用“Login()”。 检查我的 dart 代码如下: - in flutter, Need to use 'Login()' in my main.dart file. Check my dart code below: 如何在main.dart中包含单独的文件 - How to include seperate file in the main.dart IOS Simulator 可以运行一个没有命名为 main.dart 的 dart 文件吗? - Can IOS Simulator run a dart file that is not named main.dart? 我在我的 main.dart 文件中使用 ChangeNotifierProvider 作为暗模式出现此错误断言失败:boolean 表达式不得为 null - i use ChangeNotifierProvider for darkmode in my main.dart file got this error Failed assertion: boolean expression must not be null 如何测试 main.dart - How to test main.dart Android Studio 替换了我 main.dart 文件中的代码 - Android Studio replaced code in my main.dart file 如何将颤振包分离到另一个文件中,然后在 main.dart 中调用它? - How do I separate a flutter package into another file then call it in main.dart? 如何将onTab信号从嵌套的有状态小部件发送到main.dart - How can I send onTab signal from nested stateful widget to main.dart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM