1.组件类
import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui show instantiateImageCodec, Codec;
import 'package:flutter/foundation.dart';
import 'package:flutter/painting.dart';
class HttpsImage extends ImageProvider<HttpsImage> {
final String url;
final double scale;
final Map<String, String>? headers;
const HttpsImage(
this.url, {
this.scale = 1.0,
this.headers,
});
@override
Future<HttpsImage> obtainKey(ImageConfiguration configuration) {
return SynchronousFuture<HttpsImage>(this);
}
static final HttpClient _httpClient = HttpClient()
..badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
Future<ui.Codec> _loadAsync(HttpsImage key) async {
assert(key == this);
final Uri resolved = Uri.base.resolve(key.url);
final HttpClientRequest request = await _httpClient.getUrl(resolved);
headers?.forEach((String name, String value) {
request.headers.add(name, value);
});
final HttpClientResponse response = await request.close();
if (response.statusCode != HttpStatus.ok) {
throw Exception('HTTP request failed, statusCode: ${response.statusCode}, $resolved');
}
final Uint8List bytes = await consolidateHttpClientResponseBytes(response);
if (bytes.lengthInBytes == 0) {
throw Exception('NetworkImageSSL is an empty file: $resolved');
}
return await ui.instantiateImageCodec(bytes);
}
@override
bool operator ==(dynamic other) {
if (other.runtimeType != runtimeType) return false;
final HttpsImage typedOther = other;
return url == typedOther.url && scale == typedOther.scale;
}
@override
int get hashCode => hashValues(url, scale);
@override
String toString() => '$runtimeType("$url", scale: $scale)';
@override
ImageStreamCompleter load(HttpsImage key, decode) {
return MultiFrameImageStreamCompleter(
codec: _loadAsync(key),
scale: key.scale,
// informationCollector: (StringBuffer information) {
// information.writeln('Image provider: $this');
// information.write('Image key: $key');
// },
);
}
}
2.调用
import 'package:flutter/material.dart';
import 'https_image.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Container(
alignment: Alignment.center,
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
image: const DecorationImage(
image: HttpsImage("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"),
fit: BoxFit.cover,
),
borderRadius: BorderRadius.circular(8),
),
),
),
);
}
}
网友评论