preview.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:typed_data';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_svg/svg.dart';
  6. import 'package:link/view/component/web_image.dart';
  7. import '../utils/size_fit.dart';
  8. class Preview extends StatefulWidget {
  9. Uint8List? chooseImage;
  10. String? urlImage;
  11. Preview({Key? key, Uint8List? memory, String? url}) : super(key: key) {
  12. if (memory != null) {
  13. chooseImage = memory;
  14. }
  15. if (url != null) {
  16. urlImage = url;
  17. }
  18. }
  19. @override
  20. State<Preview> createState() => _PreviewState();
  21. }
  22. class _PreviewState extends State<Preview> {
  23. double w = 1.0;
  24. double h = 1.0;
  25. String url = '';
  26. bool showImage = false;
  27. @override
  28. void initState() {
  29. calulate();
  30. super.initState();
  31. }
  32. Future calulate() async {
  33. if (widget.chooseImage != null) {
  34. url = "data:image/png;base64," + base64Encode(widget.chooseImage!);
  35. var decodedImage = await decodeImageFromList(widget.chooseImage!);
  36. setState(() {
  37. h = w * decodedImage.height / decodedImage.width;
  38. showImage = true;
  39. });
  40. }
  41. if (widget.urlImage != null) {
  42. url = widget.urlImage!;
  43. Image image = Image.network(url);
  44. image.image
  45. .resolve(const ImageConfiguration())
  46. .addListener(ImageStreamListener((ImageInfo info, bool isSync) {
  47. setState(() {
  48. h = w * info.image.height / info.image.width;
  49. showImage = true;
  50. });
  51. }));
  52. }
  53. }
  54. @override
  55. Widget build(BuildContext context) {
  56. SizeFit.initialize(context);
  57. bool needScroll = false;
  58. if (h * 250.px > MediaQuery.of(context).size.height) {
  59. needScroll = true;
  60. }
  61. return Container(
  62. color: const Color(0xBF000000),
  63. width: MediaQuery.of(context).size.width,
  64. height: MediaQuery.of(context).size.height,
  65. child: Stack(children: [
  66. Positioned(
  67. top: 0,
  68. right: 0,
  69. bottom: 0,
  70. left: 0,
  71. child: GestureDetector(
  72. onTap: () {
  73. Navigator.of(context).pop();
  74. },
  75. child: Container(
  76. width: MediaQuery.of(context).size.width,
  77. height: MediaQuery.of(context).size.height,
  78. color: Colors.transparent,
  79. ),
  80. )),
  81. Positioned(
  82. right: 24.px,
  83. top: 24.px,
  84. child: GestureDetector(
  85. onTap: () {
  86. Navigator.of(context).pop();
  87. },
  88. child: SvgPicture.asset(
  89. 'assets/icons/close.svg',
  90. width: 24.px,
  91. height: 24.px,
  92. ),
  93. )),
  94. if (!needScroll && showImage)
  95. Container(
  96. alignment: Alignment.center,
  97. child: WebImage(
  98. url: url,
  99. width: 250.px,
  100. height: h * 250.px,
  101. ),
  102. ),
  103. if (needScroll && showImage)
  104. Positioned(
  105. left: 62.px,
  106. width: 250.px,
  107. bottom: 0.px,
  108. top: 0.px,
  109. child: SingleChildScrollView(
  110. child: Column(
  111. mainAxisAlignment: MainAxisAlignment.center,
  112. children: [
  113. SizedBox(
  114. height: 72.px,
  115. ),
  116. WebImage(
  117. url: url,
  118. width: 250.px,
  119. height: h * 250.px,
  120. ),
  121. SizedBox(
  122. height: 72.px,
  123. ),
  124. ],
  125. ),
  126. ))
  127. // SingleChildScrollView(
  128. // child: ,
  129. // ),
  130. ]),
  131. );
  132. }
  133. }