前端学习--flutter踩坑

1、创建动态二维码

(1)插件:qr_flutter: ^3.2.0
(2)使用方法:

QrImage(
 	padding:
  	EdgeInsets.zero,
  	data: qrcodeUrl, //地址
  	version:
  	QrVersions.auto,
  	size: 300.0,
),

在这里插入图片描述

2、flutter项目调用微信支付

(1)插件fluwx: ^2.1.0
(2)使用方法:

方法一:需要自己在页面中定义监听器来监听微信支付状态

page.dart:
//定义微信支付状态监听方法
StreamSubscription<fluwx.BaseWeChatResponse> streamSubscription;
init(){
	streamSubscription = fluwx.weChatResponseEventHandler.listen((statusData) async {
      if(statusData is fluwx.WeChatPaymentResponse){		//注意:一定要按照这种方法去定义statusData,否则是不能收到监听数据
        if(statusData.errCode==-2){
          Toast.show('您取消了支付', context,gravity:1);
        }else if(statusData.errCode==-1){
          Toast.show('支付失败:${statusData.errStr}', context,gravity:1);
        }else if(statusData.errCode==0){
          Toast.show('支付成功', context,gravity:1);
        }
      }
    });
}


//当页面离开时要清除监听器
@override
  void dispose() {
    super.dispose();
    if(streamSubscription!=null)streamSubscription.cancel();
  }

//页面调起微信支付
var result = await goPay(
    appId: wechatData.appid,
    partnerId: wechatData.partnerid,
    prepayId: wechatData.prepayid,
    nonceStr: wechatData.noncestr,
    packageValue: wechatData.package,
    timeStamp: int.parse(wechatData.timestamp),
    sign: wechatData.sign,
    signType: "MD5");


weChat.dart:
import 'package:fluwx/fluwx.dart' as fluwx;

Future<bool> goPay({
  String appId,
  String partnerId,
  String prepayId,
  String nonceStr,
  String packageValue,
  int timeStamp,
  String sign,
  String signType='',
  String extData='',
}) async {
  try{
    if(isRegister!=true)await fluwx.registerWxApi(appId:appId,universalLink: "https://your.univerallink.com/link/");
    bool isWeChatInstalled = await fluwx.isWeChatInstalled;
    if(!isWeChatInstalled){
      throw ApiError(message: '支付失败,你还没有安装微信');
    }
    var result = await fluwx.payWithWeChat(
        appId: appId,
        partnerId: partnerId,
        prepayId: prepayId,
        packageValue: packageValue,
        nonceStr: nonceStr,
        timeStamp: timeStamp,
        sign: sign,
        signType: signType,
        extData: extData
    );

    return result;

  }catch(e){
    if(e is ApiError){
      throw e;
    }else{
      throw ApiError(message: '支付失败,${e.toString()}');
    }
  }
}
方法二:通过异步方法监听微信支付状态
page.dart:
var result = await goPay(		//根据result的值来判断是否微信支付状态
          appId: wechatData.appid,
          partnerId: wechatData.partnerid,
          prepayId: wechatData.prepayid,
          nonceStr: wechatData.noncestr,
          packageValue: wechatData.package,
          timeStamp: int.parse(wechatData.timestamp),
          sign: wechatData.sign,
          signType: "MD5");



weChat.dart:
//异步返回微信支付状态
Future<String> wechatGoPay({
  String appId,
  String partnerId,
  String prepayId,
  String nonceStr,
  String packageValue,
  int timeStamp,
  String sign,
  String signType='',
  String extData='',
}) async {
  try{
    if(isRegister!=true)await fluwx.registerWxApi(appId:appId,universalLink: "https://your.univerallink.com/link/");
    bool isWeChatInstalled = await fluwx.isWeChatInstalled;
    if(!isWeChatInstalled){
      throw ApiError(message: '支付失败,你还没有安装微信');
    }

    StreamSubscription<fluwx.BaseWeChatResponse> streamSubscription;

    final com = Completer<String>();
    final future = com.future;

    streamSubscription = fluwx.weChatResponseEventHandler.listen((statusData) async {
      print('xxxxxx');
      if(statusData is fluwx.WeChatPaymentResponse){
        if(statusData.errCode==-2){
          print('您取消了支付');
          com.complete('您取消了支付');
          if(streamSubscription!=null)streamSubscription.cancel();
        }else if(statusData.errCode==-1){
          print('支付失败');
          com.complete('支付失败');
          if(streamSubscription!=null)streamSubscription.cancel();
        }else if(statusData.errCode==0) {
          print('支付成功');
          com.complete('支付成功');
          if(streamSubscription!=null)streamSubscription.cancel();
        }
      }
    });


    var result = await fluwx.payWithWeChat(
        appId: appId,
        partnerId: partnerId,
        prepayId: prepayId,
        packageValue: packageValue,
        nonceStr: nonceStr,
        timeStamp: timeStamp,
        sign: sign,
        signType: signType,
        extData: extData
    );
    return future;

  }catch(e){
    if(e is ApiError){
      throw e;
    }else{
      throw ApiError(message: '支付失败,${e.toString()}');
    }
  }
}

3、在flutter项目中打开网页地址

(1)插件: url_launcher: ^5.4.10
(2)使用方法:

  onTap: (){
     launch(url);	//url:网页地址
   },
   child: Container(       
     child: Text("打开网页地址",style: TextStyle(
         fontSize: 28/sspx,
         color: Colors.red
     ),),
   ),
 )

4、在flutter项目中使用图表
(1)插件:flutter_echarts: ^1.3.5
(2)使用方法

Container(
  child: Echarts(
    option: jsonEncode({
      "xAxis":{		//X轴数据
        "type":"category",
        "data":["Mon","Tue","Wed","Thu","Fri","Sat","Sun"],
      },
      "yAxis":{		//Y轴数据
        "type":"value"
      },
      "series":[{	//对应点得值
        "data":[820,932,901,934,1290,1330,1320],
        "type":"line"
      }]
    }),
  ),
  width: 300,
  height: 250,
)

在这里插入图片描述

4、调用支付宝APP进行支付

插件:tobias: ^1.7.1+3

//引用插件
import 'package:tobias/tobias.dart';

//调用插件
Map payResult;
payResult = await aliPay(payInfo); //payInfo为服务端返回的支付信息,格式为字符串

5、flutter使用阿里云上传图片

插件:image_picker: ^0.6.7+22

//获取图片数据
todoUpload(String result)async{
  Uint8List imageBytes;
  PickedFile cameraImage;
  try{
    startLoading(context,text: "请稍候");
    //可从直接拍照或本地相册获取图片数据
    if(result=="camera"){
      cameraImage = await new ImagePicker().getImage(source: ImageSource.camera);
      if(cameraImage==null)return;
      imageBytes = await cameraImage.readAsBytes();         
    }else if(result=="gallery"){
      cameraImage = await new ImagePicker().getImage(source: ImageSource.gallery);
      if(cameraImage==null)return;
      imageBytes = await cameraImage.readAsBytes();    
    } else{
      return;
    }
    if(imageBytes!=null){
      String data = await Http().uploadImageBytes(imageBytes);
    }
  }catch(e){       
    Toast.show(e.toString(), context);
  }
}


Future<String> uploadImageBytes(Uint8List dataBytes) async {
    var response;
    try {
      var _sign = Http.ossSignEntity;
      DateTime now = new DateTime.now();
      if(_sign==null||_sign.expireTime.difference(now).inMilliseconds<=0) {
        response = await post("$taskServerApi/photo/get_policy?token=${getToken()}");
        var baseEntity = new TextEntity(response.data);
        _sign = new OssSignEntity(baseEntity.data);
        handleTime(baseEntity,_sign);
        Http.ossSignEntity = _sign;
      }
      BaseOptions options = new BaseOptions(
        connectTimeout: 1000 * 30,
        receiveTimeout: 1000 * 30,
      );
      Dio dio = new Dio(options);
      dio.options.responseType = ResponseType.plain;
      dio.options.contentType = ContentType.parse("multipart/form-data").toString();
      var filename = md5.convert(dataBytes).toString() + DateTime.now().millisecondsSinceEpoch.toString() + '.jpg';
      FormData formData = new FormData.fromMap({
        'chunk': '0',
        'OSSAccessKeyId': _sign.accessid,
        'policy': _sign.policy,
        'Signature': _sign.signature,
        'Expires': _sign.expire,
        "callback": _sign.callback,
        'key': _sign.dir + filename,
        'success_action_status': '200',
        'Access-Control-Allow-Origin': '*',
        'file': MultipartFile.fromBytes(dataBytes,
            filename: filename,
            contentType: MediaType.parse("image/jpg"))
      });
      try {
        Response response = await dio.post(_sign.host, data: formData);
        if (response.statusCode == 200) {
          return _sign.host + '/' + _sign.dir + filename;
        } else {
          Http.ossSignEntity = null;
          throw ApiError(message: '传入OSS失败');
        }
      } catch (e) {
        Http.ossSignEntity = null;
        throw handleApiError(e);
      }
    } catch (e) {
      Http.ossSignEntity = null;
      if(e is ApiError){
        throw e;
      }
      throw ApiError(message:"获取oss配置失败${e}");
    }
  }

6、使用百度SDK获取定位信息,偶尔会出现定位数据全部为null或者经纬度正常详细地址为null的情况

https://blog.csdn.net/weixin_38009285/article/details/117361435

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值