Flutter Inject parts of Widget into parent preserving controllers and state - flutter

I am looking for a way to build a widget that creates a scroll controller and contains several widgets that all depend on that scroll controller.
Instead of rendering the whole widget I want to place the widgets that it contains inside a parent widget in their corresponding places whilst still connected to that scroll controller.
EDIT//
In laymen's terms, I want to declare a widget wherever I happen to declare it but not actually render it there. It should render instead in a targeted location but still maintain any state or controllers it obtained or inherited from the position in which it was actually declare. IF this can be done it is probably far simpler than what I have tried to outline here.
//
// Parent Widget
class ParentReceiver extends StatelessWidget {
const ParentReceiver({super.key});
Widget build(BuildContext context){
return Stack(
children: [
Align(
alignment: Alignment.topCenter,
child: // expects to receive InjectWidget's MyHeaderWidget() here.
),
// expects to receive InjectWidget's MyContentWidget() here.
Align(
alignment: Alignment.bottomCenter,
child: // expects to receive InjectWidget's MyFooterWidget() here.
),
]
);
}
}
class InjectWidget extends StatelessWidget {
const InjectWidget({super.key});
Widget build(BuildContext context){
ScrollController scrollController = useScrollController();
// yes that's a hook but it doesn't matter how I got the scroll controller.
return Stack(
children: [
MyHeaderWidget(scrollController: scrollController);
MyContentWidget(scrollController: scrollController);
MyFooterWidget(scrollController: scrollController);
],
);
}
}
class MyContentWidget extends StatelessWidget {
const MyContentWidget({super.key, required scrollController,});
final ScrollController scrollController;
Widget build(BuildContext context){
return ListView(
controller: scrollController,
children: [
etc...
]
);
}
}
So MyHeaderWidget, MyFooterWidget and MyContentWidget all used the scroll controller that was created by InjectWidget. However, InjectWidget is not to render directly. These three individual widgets with their scroll controller intact should be placed into the corresponding areas of the ParentReceiver Widget.
The basic premise behind this is to build entire widgets that respond to controllers but place various component parts of them into the parent view.

Related

Pass a variable to another widget [flutter]

Summing up the situation, I'm making a simple App in Flutter, which displays a List of Items you've added (I won't detail the app, as it would be unnecessary).
The file I created (log.dart) has a Property Class
class LogItem { /* code and stuff inside */ }
And it has a List with items
List<LogItem> itemsList = [test01, test02];
I created a simple Widget to display data for each item in this List
class SpecificItem extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: Row(
children: [
Text(
" R\$ ${itensList[i].price}",
),
Spacer(),
Text(
"${itensList[i].title}",
),
],
),
);
}
}
Just below in another widget, I created a for loop to make a variable "i" change, to display different items from this list.
class LogGraphical extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: ListView(
children: [
for (int item = 0; item < itensList.length; item++) SpecificItem(item),
],
),
);
}
}
Can someone explain to me exactly how I do the Widget ACCEPT PARAMETERS and change it? In this way creating multiple items?
I tried in many ways, but I could never get it to work!
(In the code I inserted here, I didn't put the Widget accepting anything.)

How to check ListView Scroll Speed in flutter using ScrollController

I am having problem with Flutter List View scrolls speed. Need to get the scroll speed whenever user scrolls list faster or slower. I Want to check the List view scroll speed(Velocity), If user scroll a list faster or slower using scroll controller then it will notify user that scroll speed.
You can try ScrollVelocityListener from this lib
class ExampleApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return ScrollVelocityListener(
onVelocity: (velocity) {
// Velocity is in pixels per millisecond
},
child: ListView(
children: [
Container(),
Container(),
Container(),
Container(),
],
),
);
}
}

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

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

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(...)
)

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