175 lines
6.3 KiB
Dart
175 lines
6.3 KiB
Dart
import 'package:cached_network_image/cached_network_image.dart';
|
|
import 'package:circle_app/util/util.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
import 'package:flutter_swiper/flutter_swiper.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:get/get_core/src/get_main.dart';
|
|
import 'package:get/get_state_manager/src/simple/get_state.dart';
|
|
import 'package:image_gallery_saver/image_gallery_saver.dart';
|
|
|
|
import 'logic.dart';
|
|
import 'dart:ui' as ui;
|
|
class SwiperPage extends StatefulWidget {
|
|
const SwiperPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<SwiperPage> createState() => _SwiperPageState();
|
|
}
|
|
|
|
class _SwiperPageState extends State<SwiperPage> {
|
|
|
|
GlobalKey _globalKey = GlobalKey();
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GetBuilder<SwiperLogic>(builder: (logic) {
|
|
return Scaffold(
|
|
appBar: null,
|
|
backgroundColor: Colors.black87,
|
|
body: GestureDetector(
|
|
behavior: HitTestBehavior.translucent,
|
|
onTap: (() {
|
|
//点击任意地方都能关闭页面,并且不影响你的滑动查看
|
|
Navigator.pop(context);
|
|
}),
|
|
child: Stack(
|
|
children: [
|
|
Container(
|
|
child: Swiper(
|
|
controller: logic.swiperController,
|
|
index: logic.index,
|
|
loop: false,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
if (logic.index == index) {
|
|
return RepaintBoundary(
|
|
key: _globalKey,
|
|
child:itemWidget(logic,index));
|
|
}
|
|
return itemWidget(logic,index);
|
|
},
|
|
|
|
onIndexChanged: (int index) {
|
|
logic.index = index;
|
|
logic.update();
|
|
},
|
|
itemCount: logic.imgList.length,
|
|
pagination: const SwiperPagination(
|
|
builder: DotSwiperPaginationBuilder(
|
|
activeColor: Color(0xFFD14CFF),
|
|
color: Colors.white)), //下面的分页小点
|
|
)),
|
|
Positioned(
|
|
bottom: Get.bottomBarHeight,
|
|
right: 15.sp,
|
|
child: InkWell(
|
|
onTap: () {
|
|
_saveLocalImage();
|
|
},
|
|
child: Container(
|
|
alignment: Alignment.center,
|
|
padding: EdgeInsets.all(5.sp),
|
|
decoration: BoxDecoration(
|
|
color: Colors.black.withOpacity(0.5),
|
|
borderRadius: BorderRadius.circular(17.5.sp)),
|
|
child: Icon(
|
|
Icons.download,
|
|
color: Colors.white,
|
|
size: 25.sp,
|
|
),
|
|
),
|
|
))
|
|
],
|
|
),
|
|
),
|
|
);
|
|
});
|
|
}
|
|
|
|
itemWidget(SwiperLogic logic,int index) {
|
|
return Container(
|
|
width: double.infinity,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Stack(
|
|
alignment: Alignment.center,
|
|
children: [
|
|
Container(
|
|
// color: Colors.red,
|
|
child: CachedNetworkImage(
|
|
imageUrl: logic.imgList[index].toString().contains('?') ? logic.imgList[index].toString().split('?').first : logic.imgList[index].toString(),
|
|
fit: BoxFit.contain,
|
|
),
|
|
),
|
|
Positioned(
|
|
left: 10.sp,
|
|
bottom: 20.sp,
|
|
child: Container(
|
|
margin:
|
|
EdgeInsets.only(top: Get.height * 0.45),
|
|
child: Text(
|
|
'微乐园APP',
|
|
style: TextStyle(
|
|
color: Colors.white, fontSize: 25.sp),
|
|
),
|
|
))
|
|
],
|
|
)
|
|
],
|
|
));
|
|
}
|
|
|
|
_saveLocalImage() async {
|
|
RenderRepaintBoundary boundary =
|
|
_globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
|
|
ui.Image image =
|
|
await boundary.toImage(pixelRatio: ui.window.devicePixelRatio);
|
|
ByteData? byteData =
|
|
await (image.toByteData(format: ui.ImageByteFormat.png));
|
|
if (byteData != null) {
|
|
final result =
|
|
await ImageGallerySaver.saveImage(byteData.buffer.asUint8List());
|
|
print(result);
|
|
// isSuccess
|
|
if (result['isSuccess']) {
|
|
showOKToast('保存成功');
|
|
} else {
|
|
showOKToast('保存失败,请检查相册权限是否开启');
|
|
}
|
|
}
|
|
}
|
|
// Widget build(BuildContext context) {
|
|
|
|
// return GetBuilder<SwiperLogic>(builder: (logic) {
|
|
//
|
|
// return Scaffold(
|
|
// appBar: null,
|
|
// backgroundColor: Colors.black87,
|
|
// body: GestureDetector(
|
|
// behavior: HitTestBehavior.translucent,
|
|
// onTap: (() {
|
|
// //点击任意地方都能关闭页面,并且不影响你的滑动查看
|
|
// Navigator.pop(context);
|
|
// }),
|
|
// child: Swiper(
|
|
// controller: logic.swiperController,
|
|
// index:logic.index,
|
|
// itemBuilder: (BuildContext context, int index) {
|
|
// print("img="+logic.imgList[index]);
|
|
// // return SizedBox(
|
|
// // width: double.infinity,
|
|
// // child: Image.network(
|
|
// // "${logic.imgList[index]}?imageView2/0/q/60|watermark/2/text/5b6u5LmQ5ZutQVBQ/font/5b6u6L2v6ZuF6buR/fontsize/2160/fill/I0ZGRkZGRg==/dissolve/50/gravity/SouthEast/dx/60/dy/120", fit: BoxFit.contain,)
|
|
// // );
|
|
// },
|
|
// itemCount: logic.imgList.length,
|
|
// pagination: SwiperPagination(), //下面的分页小点
|
|
// // control: new SwiperControl(), //左右的那个箭头,在某模拟器中会出现蓝线
|
|
// ),
|
|
// ),
|
|
// );
|
|
// });
|
|
// }
|
|
} |