Make the Floating Action Button to a fixed location - flutter

I'm developing an application with a FAB on the navigation bar, and when the keyboard appears FAB goes up with the keyboard. I want FAB to stay in the navigation bar as a fixed location.
Normal UI
With Keyboard
I've tried using keyboard_visibility: ^0.5.6 as mentioned in the earlier question. Flutter: Floating action button fixed location
However this plugin is deprecated, so I'm not able to use it.
This is the code related to FAB button.
#override
Widget build(BuildContext context) => Scaffold(
extendBody: true,
body: IndexedStack(
children: pages,
index: index,
),
bottomNavigationBar: TabBarMaterialWidget(
index: index,
onChangedTab: onChangedTab,
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () => print('Hello'),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
void onChangedTab(int index) {
setState(() {
this.index = index;
});
}
What needs to be done to solve this ? (If there is any other plugin like the above please let me know how to use it to solve this issue / or any other method)

From the docs:
bool? resizeToAvoidBottomInset
if there is an onscreen keyboard displayed above the scaffold, the
body can be resized to avoid overlapping the keyboard, which prevents
widgets inside the body from being obscured by the keyboard.
return Scaffold(
resizeToAvoidBottomInset: false,
...

you have to put the FAB inside the bottomNavigationBar then it will be located at the bottom always.

Related

Howto hide the keyboard in flutter when tapping outside of the TextField without triggering any other actions?

Hi I have the following (simplified) code:
class Example extends StatelessWidget {
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => FocusScope.of(context).focusedChild?.unfocus(),
child: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
print("Button pressed!");
},
child: Text("Click me"),
),
TextField(),
],
),
),
);
}
}
This code results in the following UI:
When I click on the blue button, "Button pressed!" gets printed and the keyboard does not disappear.
The problem here, is that this behavior is not what I want. I want that the keyboard disappears when I click somewhere outside of the TextField, and that the button does not get triggered, even when I directly click on it. So for example if I click on the button just the keyboard should disappear without any other action/side effect (nothing gets printed in this case). But it should still be possible to interact with the TextField normally.
Note: Disabling the button is not a good option since in my real case scenario the page is build out of a lot complex widgets and disabling them is really complicated.
Already stuck there for a while now. Hope you can help me :)
Wrap the Scaffold with Gesture detector and on tap unfocus the focus scope
example
class Test extends StatelessWidget {
const Test({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: const Scaffold(),
);
}
}
Wrap your Scaffold Widget in a Button it can be Gesture Detector or Inkwell. Then add unfocus method in it.
GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: const Scaffold(
body: TextFormField(
decoration: OutlineInputBorder()),
);
I also needed to hide the keyboard when I clicked on a date picker (another widget)
During my experience, with GestureDetector keyboard hid itself when I clicked somewhere that is not a widget. It would not hide itself otherwise.
Simply changing GestureDetector
to
Listener and adding
onPointerDown: (PointerDownEvent event) => FocusManager.instance.primaryFocus?.unfocus(), gave me the solution.
So, in your instance, what you should do is, wrap the Button "Click me" with a Listener and give it onPointerDown ,that should hide your keyboard whenever you press the button.

get on which widget is tapped when adding widgets dynamically in a stack flutter

I am trying to add widgets dynamically in a stack like adding text fields on the screen. I am adding that. But on tapping a particular widget i want to rebuilt the that widget in stack. For that I want to get on which widget is tapped in stack. How can I do that.?
List<Widget> customizedShirt = [];
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body:Stack(
children: customizedShirt,
),
floatingActionButton: FloatingActionButton(
onPressed: (){
customizedShirt.add(Positioned(
//New position for every new text in stack
child:Text("New Text")));
setstate((){});
},
child: const Icon(Icons.add),
),
);
}
}
This is an example code now on tapping a particular text I want to rebuilt that text (Like changing its font, size etc) without changing the other texts in stack

Return data from a screen on Flutter, without using Navigator.pop

I have a DetailPage that must return something to the code that called it. The documentation Return data from a screen explains that you must call Navigator.pop(context, whateverYouMustReturn) on some button.
So far so good, but what happens if the user clicks on the back button on the AppBar instead?? How do I return something then??
PS I'm using Navigator 1.0, btw
Provide your own leading widget and override the callback.
AppBar(
leading: BackButton(
onPressed: () {
Navigator.of(context).pop(myData);
},
),
),
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: Container(), //Make leading an empty container to hide the default back button
),
body: WillPopScope(
onWillPop: () async {
Navigator.pop(context); //This method returns future boolean, based on your condition you can either
return true; //allow you to go back or you can show a toast and restrict in this page
},
child: Container(), //Your details page widget instead of container
),
);
}

Floating action button to be ontop of drawer after opening

I have a floating action button which opens the drawer. I want it to stay in its place when it opens the drawer and just change the icon to close icon with animation. I couldn't find a good solution to do this.
I tried wrapping inside my drawer with scaffold and adding a floating action button there and using hero widgets so make it look like it stays in its place but didn't help at all.
Is there anyway to achieve this?
U can use foldable Sidebar package to achieve this functionalities that's package is available on :
https://pub.dev/packages/foldable_sidebar
And check this piece of code how to use this :
child: Scaffold(
body: FoldableSidebarBuilder(
drawerBackgroundColor: Colors.deepOrange,
drawer: CustomDrawer(closeDrawer: (){
setState(() {
drawerStatus = FDBStatus.FDB_CLOSE; // For Closing the Sidebar
});
},),
screenContents: FirstScreen(), // Your Screen Widget
status: drawerStatus,
),
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.deepOrange,
child: Icon(Icons.menu,color: Colors.white,),
onPressed: () {
// To Open/Close Sidebar
setState(() {
drawerStatus = drawerStatus == FDBStatus.FDB_OPEN ? FDBStatus.FDB_CLOSE : FDBStatus.FDB_OPEN;
});
}),
),
),

How do I make bottomsheet swipe up from anywhere in scaffold flutter

I am trying to make a launcher in flutter, however I cannot figure out how to make a drawer that can be swiped up from the home screen, like in other launchers like nova or poco or many others. I understand that the app drawer closely matches a bottom sheet, but bottom sheets in flutter need to be first tapped on them and then dragged. How do I drag a widget up from anywhere on the scaffold?
To obtain the functionality you're looking for use this package: flutter slider
Example:
with code as simple as this you can obtain the slideup bottom sheet like functionality:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("SlidingUpPanelExample"),
),
body: SlidingUpPanel(
panel: Center(
child: Text("This is the sliding Widget"),
),
body: Center(
child: Text("This is the Widget behind the sliding panel"),
),
),
);
}