Skip to content

Instantly share code, notes, and snippets.

@drstranges
Last active February 18, 2021 15:59
Show Gist options
  • Save drstranges/5f9e7e1651762c2abf8f4cb97d1ed745 to your computer and use it in GitHub Desktop.
Save drstranges/5f9e7e1651762c2abf8f4cb97d1ed745 to your computer and use it in GitHub Desktop.
No scroll on add items on top of SliverList
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'No scroll on add items on top of SliverList';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: MainWidget(),
);
}
}
class MainWidget extends StatefulWidget {
MainWidget({Key key}) : super(key: key);
@override
_MainWidgetState createState() => _MainWidgetState();
}
class _MainWidgetState extends State<MainWidget> {
List<int> top = [];
@override
Widget build(BuildContext context) {
const Key centerKey = ValueKey('anchor');
return Scaffold(
appBar: AppBar(
title: const Text('Press on the plus to add items above'),
leading: IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() {
top.add(top.length);
});
},
),
),
body: CustomScrollView(
center: centerKey,
slivers: <Widget>[
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
alignment: Alignment.center,
color: Colors.blue[200 + top[index] % 4 * 100],
height: 100 + top[index] % 4 * 20.0,
child: Text('Item: -${index + 1}'),
);
},
childCount: top.length,
),
),
SliverToBoxAdapter(
key: centerKey,
child: Text("New items above"),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment