How to implement Scroll Controller in Flutter (Scroll Widget)? - flutter

I am trying to implement a Scroll Widget in Flutter but not able to implement it.
I saw many tutorials but they have implemented scroll widget with ListView or Grid View.
In my case, I have Register page with multiple Text Inputs and Buttons. In this case, my page is not scrolling down please help someone.
div{
Scroll Widget on the Register page.
}

like this
class ExampleScroll extends StatefulWidget {
#override
_ExampleScrollState createState() => _ExampleScrollState();
}
class _ExampleScrollState extends State<ExampleScroll> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: SingleChildScrollView(
child: Container(),
),
);
}
}

Please wrap with a SingleChildScrollView Widget.
Like this...
class AppDashBoard extends StatelessWidget {
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Container(
child: Text('It Scrollable'),
),
);
}
}

You can use SingleChildScrollView for scrolling like this.
SingleChildScrollView(
child:Column(...)
)

You can use SingleChildScrollView and provide scrolling contents as its child
for more you can find on the documentation
https://api.flutter.dev/flutter/widgets/SingleChildScrollView-class.html
Eg:
SingleChildScrollView(
// scrollable contents as child
child:Column(...)
)

Related

Screen independent way to detect clicks outside a widget

I'm looking for a screen independent way to detect clicks outside a widget.
Normally to detect clicks outside a widget I would need to wrap my screen around a GestureDetector with behavior: HitTestBehavior.opaque (or something like that).
Example:
#override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () => handleClickOutside(),
child: Container(
child: MyWidget(),
),
),
);
}
The problem is that if I want to use this widget anywhere, I would have to wrap every screen...
So my question is, is there a way to achieve what I'm trying to do without the mandatory screen wrap?
You can reduce the amount of boiler plate code by creating you own widget that collects all the stuff you need on every page:
class MyPage extends StatelessWidget {
final Widget child;
MyPage({super.key, required this.child});
#override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: handleClickOutside,
child: child,
),
);
}
void handleClickOutside() {
// here is where you handle the click outside any widget
}
}
Note that you Container in you sample is not necessary.
Note the use of a method tear-off for the onTap parameter

Idiomatic way to change aspects of the active scaffold from within Flutter TabView child

Essentially, I'm trying to devise a way for a child of a TabView to customise aspects of the active Scaffold instance, such as the floating action button, application bar and so on.
Using the code snippet below, the idea is for_ATabState to set the floatingActionButton of the Scaffold instance to a custom widget that it controls.
class MasterWidget extends StatefulWidget {
#override
_MasterWidgetState createState() => _MasterWidgetState();
Widget? setFloatingActionButton() {
// how to access state and invoke `setState`?
}
}
class _MasterWidgetState extends State<MasterWidget> {
#override
Widget build(BuildContext context) {
final tabs = [ATab()];
return Scaffold(
appBar: AppBar(),
floatingActionButton: activeTab.buildFloatingActionButton(),
body: DefaultTabController(
length: tabs.length,
child: TabBarView(
children: tabs,
controller: tabController,
),
),
);
}
}
class ATab extends StatefulWidget {
#override
_ATabState createState() => _ATabState();
}
class _ATabState extends State<ATab> {
#override
Widget build(BuildContext context) {
// Doesn't work:
// context.findAncestorWidgetOfExactType<Scaffold>()?.floatingActionButton = AFloatingActionButton();
// context.findAncestorWidgetOfExactType<MasterWidget>()?.setFloatingActionButton(AFloatingActionButton());
return SomeWidget();
}
}
Here's what I tried:
Try to context.findAncestorWidgetOfExactType<Scaffold>() in _ATabState and somehow set Scaffold's floatingActionButton attribute; unfortunately there does not seem to be a setter available.
Try to context.findAncestorWidgetOfExactType<MasterWidget>() in _ATabState but then I'm not able to access the state where the rendering takes place.
What's the approach applicable here?

Is there a way in Flutter to navigate to a page gradually by sliding

Here is my problem.
I would like that by dragging progressively (with my finger for example) up a container (Container) that this one moves following the movement but that a new page (NewPage) towards which I want to navigate is supperposed on the container while following the movement until a certain level when I release, that the page takes all the screen. (Let's say I want to do a Navigator.push but in this sense).
Thank you.
class NewPage extends StatelessWidget {
...
#override
Widget build(BuildContext context) {
return Scaffold(...);
}
}
...
class _MainPageState extends State<MainPage> {
...
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
...
GestureDetector(
child: Positioned(
...
child: Container()
)
)
]
)
);
}
}
Your question is a bit unclear. But try these different navigation techniques.
Hero Animations
PageView - Try this with scrollDirection: Axis.vertical

ClampingScrollPhysics when setted NeverScrollableScrollPhysics

I have made PageView, set physics to NeverScrollableScrollPhysics to disabled scroll and implements it by buttons. But when I call controller.animateToPage(/* ... */), my PageView animated with scroll glow. How to disabled this behaviour? Or this is bug of Flutter itself?
To disable Scroll glow, create a class that extends ScrollBehavior
class DisableScrollGlowBehavior extends ScrollBehavior {
#override
Widget buildViewportChrome(
BuildContext context, Widget child, AxisDirection axisDirection) {
return child;
}
}
Then wrap your scrollable List widget with ScrollConfiguration widget using the behavior of the class created.
ScrollConfiguration(
behavior: DisableScrollGlowBehavior(),
child: ListView(
children: [],
),
)

How to add multiple rows or columns after the body

I'm still trying to understand how to structure widgets. I have placed a container in the body already so how can I now add another row. I've removed some code to simplify my situation but hopefully this gives an idea of how my project is structured at the moment.
class AddButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: Alignment.bottomCenter,
//Stats Button
child: Row(
), //container
//How can I enter a new row here <------- WHERE I WANT TO ENTER A ROW
);
}
}
The short answer is, you cannot. You can take advantage of the children property of a Column, the most common layout widget in all of Flutter. Flutter works on a system of nested widgets, you cannot have many parents as it all starts with one widget.
class AddButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: Alignment.bottomCenter,
//Stats Button
child: Column(
children: <Widget>[
Row(
children: <Widget>[
// nested widgets
],
),
],
),
),
);
}
}
Judging by your class name, you just want a button. Not every widget starts with a Scaffold, that's only if you want an entire layout with an app bar or a bottom navigation bar. For simple widgets like a button, you can get rid of Scaffold entirely and just use MaterialButton like this.
class AddButton extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialButton(
onPressed: () {}, //empty function
);
}
}
More reading:
https://flutter.dev/docs/development/ui/layout
https://pusher.com/tutorials/flutter-building-layouts
https://medium.com/flutter-community/flutter-layout-cheat-sheet-5363348d037e