Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/rendering.dart';
  3.  
  4. void main() => runApp(new MyApp());
  5.  
  6. class MyApp extends StatefulWidget {
  7. @override
  8. State<StatefulWidget> createState() => new MyAppState();
  9. }
  10.  
  11. class MyAppState extends State<MyApp> {
  12. GlobalKey<State> key = new GlobalKey();
  13.  
  14. double fabOpacity = 1.0;
  15.  
  16. @override
  17. Widget build(BuildContext context) {
  18. return new MaterialApp(
  19. home: new Scaffold(
  20. appBar: new AppBar(
  21. title: new Text("Scrolling."),
  22. ),
  23. body: NotificationListener<ScrollNotification>(
  24. child: new ListView(
  25. itemExtent: 100.0,
  26. children: [
  27. ContainerWithBorder(),
  28. ContainerWithBorder(),
  29. ContainerWithBorder(),
  30. ContainerWithBorder(),
  31. ContainerWithBorder(),
  32. ContainerWithBorder(),
  33. ContainerWithBorder(),
  34. ContainerWithBorder(),
  35. new MyObservableWidget(key: key),
  36. ContainerWithBorder(),
  37. ContainerWithBorder(),
  38. ContainerWithBorder(),
  39. ContainerWithBorder(),
  40. ContainerWithBorder(),
  41. ContainerWithBorder(),
  42. ContainerWithBorder()
  43. ],
  44. ),
  45. onNotification: (ScrollNotification scroll) {
  46. var currentContext = key.currentContext;
  47. if (currentContext == null) return false;
  48.  
  49. var renderObject = currentContext.findRenderObject();
  50. RenderAbstractViewport viewport = RenderAbstractViewport.of(renderObject);
  51. var offsetToRevealBottom = viewport.getOffsetToReveal(renderObject, 1.0);
  52. var offsetToRevealTop = viewport.getOffsetToReveal(renderObject, 0.0);
  53.  
  54. if (offsetToRevealBottom.offset > scroll.metrics.pixels ||
  55. scroll.metrics.pixels > offsetToRevealTop.offset) {
  56. if (fabOpacity != 1.0) {
  57. setState(() {
  58. fabOpacity = 1.0;
  59. });
  60. }
  61. } else {
  62. if (fabOpacity == 1.0) {
  63. setState(() {
  64. fabOpacity = 0.0;
  65. });
  66. }
  67. }
  68. return false;
  69. },
  70. ),
  71. floatingActionButton: new Opacity(
  72. opacity: fabOpacity,
  73. child: Align(
  74. alignment: Alignment.bottomCenter,
  75. child: new FloatingActionButton.extended(
  76. label: Text('sticky buy button'),
  77. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.0)),
  78. onPressed: () {
  79. print("YAY");
  80. },
  81. ),
  82. ),
  83. ),
  84. ),
  85. );
  86. }
  87. }
  88.  
  89. class MyObservableWidget extends StatefulWidget {
  90. const MyObservableWidget({Key key}) : super(key: key);
  91.  
  92. @override
  93. State<StatefulWidget> createState() => new MyObservableWidgetState();
  94. }
  95.  
  96. class MyObservableWidgetState extends State<MyObservableWidget> {
  97. @override
  98. Widget build(BuildContext context) {
  99. return new RaisedButton(
  100. onPressed: () {
  101.  
  102. },
  103. color: Colors.lightGreenAccent,
  104. child: Text('This is my buy button', style: TextStyle(color: Colors.blue),),
  105. );
  106. }
  107. }
  108.  
  109. class ContainerWithBorder extends StatelessWidget {
  110. @override
  111. Widget build(BuildContext context) {
  112. return new Container(
  113. decoration: new BoxDecoration(border: new Border.all(), color: Colors.grey),
  114. );
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement