This commit is contained in:
bluesea 2024-05-14 15:48:00 +08:00
parent 6d393401c0
commit d49d39165f
8 changed files with 81 additions and 34 deletions

View File

@ -1,11 +1,10 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:wallpaper/utils/downloadmanager.dart';
// import 'TestApp.dart'; // import 'TestApp.dart';
import 'WallPaperApp.dart'; import 'WallPaperApp.dart';
Future<void> main() async { void main() {
await DownloadManager.downloadImgWithUrl("https://resource-sg-public.lux-ad.com/wallpaper/4f1e7f66f26003b0ea83f4934302ce84.jpg");
runApp(const WallPaperApp()); runApp(const WallPaperApp());
} }

View File

@ -1,4 +1,5 @@
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:wallpaper/routes/app_routes.dart';
import "package:wallpaper/utils/assettool.dart"; import "package:wallpaper/utils/assettool.dart";
import 'package:wallpaper/models/imgnetodel.dart'; import 'package:wallpaper/models/imgnetodel.dart';
class HomePageController extends GetxController { class HomePageController extends GetxController {
@ -9,10 +10,15 @@ class HomePageController extends GetxController {
super.onReady(); super.onReady();
setupTypeListData(); setupTypeListData();
} }
void setupTypeListData() async{ void setupTypeListData() async{
var temData = await loadWallpaperJsonFromAsset(); var temData = await loadWallpaperJsonFromAsset();
typeList.value = temData.map((e) => NetImgCategory.fromJson(e)).toList(); typeList.value = temData.map((e) => NetImgCategory.fromJson(e)).toList();
} }
void goCategoryView(var title,var dataModel){
Get.toNamed(AppRoutes.imgcategorypage, arguments: {"stitle":title,"dataModel":dataModel});
}
} }

View File

@ -62,7 +62,8 @@ class HomePageView extends GetView <HomePageController> {
color: Colors.transparent, color: Colors.transparent,
child: InkWell( child: InkWell(
onTap: () { onTap: () {
Get.to(ImgCategoryPageView(stitle: title,dataModel: controller.typeList[index])); // Get.to(ImgCategoryPageView(stitle: title,dataModel: controller.typeList[index]));
controller.goCategoryView(title, controller.typeList[index]);
}, },
child: Align( child: Align(
alignment: Alignment.bottomCenter, alignment: Alignment.bottomCenter,

View File

@ -1,5 +1,22 @@
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:wallpaper/models/imgnetodel.dart';
import 'package:wallpaper/utils/downloadmanager.dart';
class ImgCategoryPageController extends GetxController { class ImgCategoryPageController extends GetxController {
var stitle = RxString("");
// ignore: prefer_typing_uninitialized_variables
late NetImgCategory dataModel;
@override
void onInit() {
super.onInit();
stitle.value = Get.arguments["stitle"];
dataModel = Get.arguments["dataModel"];
}
Future < void> downloadImg(String urlStr) async {
await DownloadManager.downloadImgWithUrl(urlStr);
//
//....
}
} }

View File

@ -1,15 +1,17 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:wallpaper/pages/imgcategorypage/imgcategorypage_controller.dart'; import 'package:wallpaper/pages/imgcategorypage/imgcategorypage_controller.dart';
import 'package:wallpaper/models/imgnetodel.dart'; // import 'package:wallpaper/models/imgnetodel.dart';
import 'package:transparent_image/transparent_image.dart'; import 'package:transparent_image/transparent_image.dart';
import 'package:wallpaper/pages/imgscanpage/imgscanpage_view.dart'; import 'package:wallpaper/pages/imgscanpage/imgscanpage_view.dart';
class ImgCategoryPageView extends GetView { class ImgCategoryPageView extends GetView<ImgCategoryPageController> {
const ImgCategoryPageView({super.key});
// const ImgCategoryPageView({super.key}); // const ImgCategoryPageView({super.key});
final String stitle; // final String stitle;
final NetImgCategory dataModel; // final NetImgCategory dataModel;
const ImgCategoryPageView({super.key, required this.stitle,required this.dataModel}); // const ImgCategoryPageView({super.key, required this.stitle,required this.dataModel});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -17,7 +19,7 @@ class ImgCategoryPageView extends GetView {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text(stitle), title: Obx(() => Text(controller.stitle.value)) ,
), ),
body: bodyView , body: bodyView ,
); );
@ -36,10 +38,19 @@ class ImgCategoryPageView extends GetView {
crossAxisSpacing: 2, crossAxisSpacing: 2,
childAspectRatio: 0.7, childAspectRatio: 0.7,
padding: const EdgeInsets.symmetric(horizontal: 5), padding: const EdgeInsets.symmetric(horizontal: 5),
children: List.generate(dataModel.data!.length, (index) { children: List.generate(controller.dataModel.data!.length, (index) {
return getBodyItem(dataModel.data![index].previewThumb!, index); return getBodyItem(controller.dataModel.data![index].previewThumb!, index);
}), }));
); // return Obx(() => GridView.count(
// crossAxisCount: 2,
// mainAxisSpacing: 2,
// crossAxisSpacing: 2,
// childAspectRatio: 0.7,
// padding: const EdgeInsets.symmetric(horizontal: 5),
// children: List.generate(controller.dataModel.data!.length, (index) {
// return getBodyItem(controller.dataModel.data![index].previewThumb!, index);
// }),
// ));
} }
Stack getBodyItem(String imgUrl,int index) { Stack getBodyItem(String imgUrl,int index) {
@ -57,7 +68,7 @@ class ImgCategoryPageView extends GetView {
color: Colors.transparent, color: Colors.transparent,
child: InkWell( child: InkWell(
onTap: () { onTap: () {
Get.to(ImgScanPageView(dataModel: dataModel,currentIndex: index,)); Get.to(ImgScanPageView(dataModel: controller.dataModel,currentIndex: index,));
}, },
child: Align( child: Align(
alignment: Alignment.bottomCenter, alignment: Alignment.bottomCenter,
@ -74,7 +85,10 @@ class ImgCategoryPageView extends GetView {
IconButton(onPressed:() { IconButton(onPressed:() {
print("download...."); print("download....");
controller.downloadImg(imgUrl);
}, icon: Icon(Icons.download),color: Colors.white,), }, icon: Icon(Icons.download),color: Colors.white,),
IconButton(onPressed:() { IconButton(onPressed:() {
print("设置壁纸"); print("设置壁纸");

View File

@ -1,5 +1,6 @@
import 'package:wallpaper/pages/homepage/homepage_binding.dart'; import 'package:wallpaper/pages/homepage/homepage_binding.dart';
import 'package:wallpaper/pages/imgcategorypage/imgcategorypage_binding.dart'; import 'package:wallpaper/pages/imgcategorypage/imgcategorypage_binding.dart';
import 'package:wallpaper/pages/imgcategorypage/imgcategorypage_view.dart';
import 'package:wallpaper/pages/imgscanpage/imgscanpage_binding.dart'; import 'package:wallpaper/pages/imgscanpage/imgscanpage_binding.dart';
import 'package:wallpaper/pages/tabarpage/tabarpage_binding.dart'; import 'package:wallpaper/pages/tabarpage/tabarpage_binding.dart';
@ -17,13 +18,17 @@ import 'package:get/get.dart';
class AppRoutes { class AppRoutes {
AppRoutes._(); AppRoutes._();
static const initialPage = '/tabvc'; static const initialPage = '/tabvc';
static const imgcategorypage = "/ImgCategoryPage";
static final routes= [ static final routes= [
GetPage( GetPage(
name: initialPage, name: initialPage,
page: ()=> const TabbarPageView(), page: ()=> const TabbarPageView(),
bindings: [TabbarPageBinding(), HomePageBinding(),DownloadPageBinding(),MyPageBinding(),ImgCategoryPageBinding(),ImgScanPageBinding()], bindings: [TabbarPageBinding(), HomePageBinding(),DownloadPageBinding(),MyPageBinding(),ImgScanPageBinding()],
participatesInRootNavigator: true, ),
GetPage(
name: imgcategorypage,
page: ()=> const ImgCategoryPageView(),
bindings: [ImgCategoryPageBinding()],
), ),
]; ];

View File

@ -1,16 +1,21 @@
import 'dart:io'; import 'dart:io';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:wallpaper/utils/local_img_manager.dart';
class DownloadManager { class DownloadManager {
static Future<void> downloadImgWithUrl(String urlStr) async { static Future<void> downloadImgWithUrl(String urlStr) async {
var httpClient = HttpClient(); var httpClient = HttpClient();
try { try {
// option: Options(responseType: ResponseType.bytes)
var reqiest = await httpClient.getUrl(Uri.parse(urlStr)); var reqiest = await httpClient.getUrl(Uri.parse(urlStr));
var response = await reqiest.close(); var response = await reqiest.close();
// var bytes = await consolidateHttpClientResponseBytes(response); // var bytes = await consolidateHttpClientResponseBytes(response);
var bytes = await consolidateHttpClientResponseBytes(response); var bytes = await consolidateHttpClientResponseBytes(response);
print("the bytes:${bytes}"); var fileName = await LocalImgManager.saveImgDataBytes(bytes);
// print(fileName);
//
//.....
} }
catch(error) { catch(error) {
print("the error:${error}"); print("the error:${error}");

View File

@ -19,13 +19,13 @@ class LocalImgManager {
String fileName = "img_${LocalImgManager.sha1RandomString()}.jpg"; String fileName = "img_${LocalImgManager.sha1RandomString()}.jpg";
var localDir = await getImgLocalDir(); var localDir = await getImgLocalDir();
var filePath = '$localDir/$fileName'; var filePath = '$localDir/$fileName';
// print("the filepath:$filePath ");
File imageFile=File(filePath); File imageFile=File(filePath);
if(imageFile.existsSync()){ if(imageFile.existsSync()){
print("图片已存在"); print("图片已存在");
return ""; return "";
} }
imageFile.writeAsBytes(data); await imageFile.writeAsBytes(data);
return fileName; return fileName;
} }