When imageCache.clear , CustomScrollView doesn't maintain image size in scrollView anymore

82 views
Skip to first unread message

Tang Hai

unread,
Mar 14, 2021, 10:42:56 PM3/14/21
to Flutter Development (flutter-dev)
I'm facing a problem, When I call PaintingBinding.instance.imageCache.clear(), CustomScrollView doesn't maintain image size in scrollView anymore.
I know the cache is cleared. but I'd like to maintain image size in CustomScrollView, How to solve it? Thanks a lot.
This is a minimum reproducible demo:


import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}

class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
ScrollController controller = ScrollController();
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: CustomScrollView(
controller: controller,
physics: BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
slivers: [
SliverToBoxAdapter(
child: Container(
color: Colors.blue[100],
height: 400,
width: double.infinity,
),
),
SliverToBoxAdapter(
child: Image.network(
width: double.infinity,
fit: BoxFit.fill,
),
),
SliverToBoxAdapter(
child: GestureDetector(
onTap: () => print(
'maxScrollExtent:${controller.position.maxScrollExtent}'),
child: Container(
color: Colors.blue[200],
height: 400,
width: double.infinity,
),
),
),
SliverToBoxAdapter(
child: GestureDetector(
onTap: () => pushPage(context),
child: Container(
color: Colors.blue[300],
height: 400,
width: double.infinity,
),
),
),
],
),
);
}

void pushPage(BuildContext context) {
Navigator.push(
context,
MaterialPageRoute<void>(
builder: (context) {
Future.delayed(Duration(seconds: 1), () {
print('clean chache');
PaintingBinding.instance.imageCache.clear();
});
return Scaffold(
appBar: AppBar(title: Text('My Page')),
body: Container(),
);
},
),
);
}
}

Reply all
Reply to author
Forward
0 new messages