[英]flutter , how to set image network as wallpaper 2021
我正在開發一個設置壁紙的應用程序,它通過鏈接“ imgUrl ”進入應用程序,但是當我想將特定圖像設置為壁紙時,它給了我這個錯誤,
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: MissingPluginException(No implementation found for method requestPermissions on channel flutter.baseflow.com/permissions/methods)
我希望你能給我一個簡單的方法來將圖像設置為牆紙,以及將圖像保存在設備內的方法,注意:我使用 flutter僅用於 Android
import 'package:flutter/material.dart';
import 'package:gallery_saver/gallery_saver.dart';
import 'package:get/get.dart';
import 'package:image_downloader/image_downloader.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:io';
import 'dart:async';
import 'dart:typed_data';
import 'package:dio/dio.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:wallpaper/wallpaper.dart';
class ImgDetails extends StatefulWidget {
@override
_ImgDetailsState createState() => _ImgDetailsState();
}
class _ImgDetailsState extends State<ImgDetails> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
var imgPath ;
String home = "Home Screen",
lock = "Lock Screen",
both = "Both Screen",
system = "System";
String _title = Get.arguments['title'];
String _imgUrl = Get.arguments['url'];
@override
Widget build(BuildContext context) {
final snackBar = SnackBar(
content: Text(_title),
duration: const Duration(seconds:3),
);
return Scaffold(
appBar: AppBar(
title: Text(_title),
centerTitle: true,
),
key: _scaffoldKey,
body: Stack(
alignment: Alignment.bottomCenter,
children: [
Container(
height: double.infinity,
child: InteractiveViewer(
maxScale: 6,
child: FadeInImage(
image: NetworkImage(_imgUrl),
placeholder: AssetImage('assets/img/img_ot_found.jpg'),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: MediaQuery.of(context).size.width*0.8,
height: MediaQuery.of(context).size.height*0.08,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.purple , Colors.purpleAccent]
)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(icon: Icon(Icons.save_alt_rounded , color: Colors.white,size: 32), onPressed: (){
}),
IconButton(icon: Icon(Icons.imagesearch_roller, color: Colors.white,size: 32), onPressed: (){
_modal();
}),
IconButton(icon: Icon(Icons.info_outline, color: Colors.white,size: 32,), onPressed: (){
_scaffoldKey.currentState.showSnackBar(snackBar);
}),
],
),
),
),
],
),
);
}
_askPermission() async {
if (Platform.isAndroid) {
await PermissionHandler().requestPermissions([
PermissionGroup.storage,
PermissionGroup.camera,
PermissionGroup.location,
]);
} else {
await PermissionHandler()
.checkPermissionStatus(PermissionGroup.storage);
}
}
_modal() {
showModalBottomSheet(
backgroundColor: Colors.white.withOpacity(0.2),
elevation: 0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30,)),
context: context,
builder: (BuildContext context) {
return Container(
height: 130,
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.8),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
)
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
_createTile(context, 'Home Screen', Icons.home, _action1),
SizedBox(height: 10,),
_createTile(context, 'Lock Screen', Icons.lock, _action2),
],
),
);
}
);
}
ListTile _createTile(BuildContext context, String name, IconData icon,
Function action) {
return ListTile(
leading: Icon(icon,
color: Colors.blueAccent,),
title: Text(name,
style: TextStyle(
color: Colors.white, fontWeight: FontWeight.w500, fontSize: 20),),
onTap: () {
Navigator.pop(context);
action();
},
);
}
_action1() async {
if (Platform.isAndroid) {
await _askPermission();
}
var response = await Dio()
.get(_imgUrl, options: Options(responseType: ResponseType.bytes));
//await ImageGallerySaver.saveImage(Uint8List.fromList(response.data));
home = await Wallpaper.homeScreen(_imgUrl);
final result = home = home;
print(result);
}
_action2() async {
if (Platform.isAndroid) {
await _askPermission();
}
var response = await Dio()
.get(_imgUrl, options: Options(responseType: ResponseType.bytes));
//await ImageGallerySaver.saveImage(Uint8List.fromList(response.data));
home = await Wallpaper.lockScreen(_imgUrl);
final result = home = lock;
print(result);
}
}
您的權限有問題。 您需要在 AndroidManifest 文件中添加權限代碼。 您能否將所有錯誤日志發送給我,以便我找出缺少的權限?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.