Skip to content

Instantly share code, notes, and snippets.

@MiniSuperDev
Created May 16, 2022 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MiniSuperDev/23cf9e1809ca9dc29d38ecf00a167f06 to your computer and use it in GitHub Desktop.
Save MiniSuperDev/23cf9e1809ca9dc29d38ecf00a167f06 to your computer and use it in GitHub Desktop.
MultiSliver has wrong behavior with center key - sliver_tools: 0.2.6
import 'package:flutter/material.dart';
import 'package:sliver_tools/sliver_tools.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: DemoApp(),
);
}
}
class DemoApp extends StatefulWidget {
const DemoApp({Key? key}) : super(key: key);
@override
_DemoAppState createState() => _DemoAppState();
}
class _DemoAppState extends State<DemoApp> {
bool multiSliverEnabled = true;
List<int> top = List<int>.generate(10, (i) => -(i));
List<int> bottom = List<int>.generate(10, (i) => i + 1);
@override
Widget build(BuildContext context) {
const Key centerKey = ValueKey('second-sliver-list');
return Scaffold(
appBar: AppBar(
title: const Text('Press Button to Toggle MultiSliver'),
),
floatingActionButton: FloatingActionButton.extended(
onPressed: () {
setState(() {
multiSliverEnabled = !multiSliverEnabled;
});
},
label: Text('MultiSliver: $multiSliverEnabled'),
),
body: CustomScrollView(
center: centerKey,
slivers: <Widget>[
if (multiSliverEnabled) ...[
MultiSliver(
children: [_buildTopList()],
),
MultiSliver(
key: centerKey,
children: [_buildBottomList()],
),
] else ...[
_buildTopList(),
_buildBottomList(centerKey),
]
],
),
);
}
Widget _buildBottomList([Key? centerKey]) {
return SliverList(
key: centerKey,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
alignment: Alignment.center,
color: Colors.blue.shade200,
child: Text('Item: ${bottom[index]}'),
);
},
childCount: bottom.length,
),
);
}
Widget _buildTopList() {
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
alignment: Alignment.center,
color: Colors.green.shade200,
child: Text('Item: ${top[index]}'),
);
},
childCount: top.length,
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment