Skip to content

Instantly share code, notes, and snippets.

@Piinks
Last active August 29, 2019 16:57
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 Piinks/55dc1366ff2b514416af5b29ae902de1 to your computer and use it in GitHub Desktop.
Save Piinks/55dc1366ff2b514416af5b29ae902de1 to your computer and use it in GitHub Desktop.
Bottom Pinned Buttons with Dynamic Scroll Physics
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Bottom Pinned Button')),
body: MyWidget(),
)));
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
int childCount = 1;
ScrollController _controller;
ScrollPhysics physics = ClampingScrollPhysics();
_addRows() {
setState(() => childCount++);
_checkPhysics();
}
_removeRows() {
if (childCount > 0) {
setState(() => childCount--);
}
_checkPhysics();
}
_checkPhysics() {
if (_controller.position.maxScrollExtent > 0) {
setState(() => physics = BouncingScrollPhysics());
} else {
setState(() => physics = ClampingScrollPhysics());
}
}
@override
void initState() {
_controller = ScrollController();
_controller.addListener(_checkPhysics);
super.initState();
}
@override
Widget build(BuildContext context) {
return CustomScrollView(
controller: _controller,
physics: physics,
slivers: <Widget>[
SliverFixedExtentList(
itemExtent: 150.0,
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return Container(
color: index % 2 == 0 ? Colors.amber[200] : Colors.blue[200],
);
},
childCount: childCount,
),
),
SliverFillRemaining(
hasScrollBody: false,
child: Container(
child: Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.all(32.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
FloatingActionButton(
onPressed: () => _removeRows(),
child: Icon(Icons.arrow_upward),
),
FloatingActionButton(
onPressed: () => _addRows(),
child: Icon(Icons.arrow_downward),
),
],
),
),
),
),
),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment