friends I want the Floating Action Button to slide when I scroll up and down the page in Floating Action Button, which widget should I use for this?
Mycode
FloatingActionButton.extended(
onPressed: () {
print("hello world");
},
label: const Text('Update',style: TextStyle(color: Colors.white),),
icon: const Icon(Icons.update_outlined,color: Colors.white,),
backgroundColor: Colors.blue,
// foregroundColor: Text(Colors.white),
),
CodePudding user response:
let's try, here we use NotificationListener
. With the help of NotificationListener
, we take the screen info and when start scrolling up or down, then we change the FloatingActionButtonLocation
.
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(debugShowCheckedModeBanner: false, home: MyHomePage()));
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key key,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
ScrollController _scrollController = ScrollController();
FloatingActionButtonLocation _floatingActionButtonLocation =
FloatingActionButtonLocation.endFloat;
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
floatingActionButton: FloatingActionButton(
child: Text("test"),
),
floatingActionButtonLocation: _floatingActionButtonLocation,
body: Container(
child: NotificationListener(
onNotification: (ScrollNotification scrollInfo) {
setState(() {
if (scrollInfo.metrics.pixels > 0.0) { // here you change the condition
_floatingActionButtonLocation =
FloatingActionButtonLocation.endTop;
} else {
_floatingActionButtonLocation =
FloatingActionButtonLocation.endFloat;
}
});
return true;
},
child: ListView(
children: [
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
SizedBox(
height: 500,
),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
Text("data"),
],
),
),
),
),
);
}
}