How use onTap function of treeview library flutter - flutter

Hi I am using this pubdev package https://pub.dev/packages/tree_view/example . My problem is that when I use the onTap function the expanded does not work, I was checking the library and I noticed that it is because of this code, however I do not know how to solve it or if there is another way to access that function from the library from the widget. Any ideas
If you notice it when the onTap function is different from empty, the toggleExpanded() function does not apply
So any ideas??

You need to control the expansion yourself using the startExpanded property of the TreeView widget.
First, you have a boolean variable (say _startExpanded) in your StatefulWidget that hold the state of the expansion and you can set it the default state (for instance, false).
bool _startExpanded = false;
Then, you pass the variable to your TreeView widget:
TreeView(
startExpanded: _startExpanded,
children: _getChildList(documentList),
),
To expand, you call:
setState((){
_startExpanded = true;
});
To close, you call:
setState((){
_startExpanded = false;
});

Related

is it possible to automatically refresh component when getx value changed flutter

I am using getx get: ^4.3.8 as the state management component in flutter, when I using this code to update the controller value:
var expand = controller.newTaskExpanded.value;
controller.newTaskExpanded(!expand);
I found the component did not refresh, then I have to invoke the getx refresh function like this:
controller.refresh();
this is the controller properties define:
class HomeController extends GetxController {
List<Widget> widgetsList = List.empty(growable: true);
List<Todo> tasks = List.empty(growable: true);
var newTaskExpanded = true.obs;
var completedTaskExpanded = false.obs;
}
is is possible to auto refresh the flutter component when controller properties value changed? should I invoke the refresh function every time change the value of getx controller?
Reactive programming with Get is as easy as using setState.
Let's imagine that you have a name variable and want that every time you change it, all widgets that use it are automatically changed.
This is your count variable:
var name = 'Jonatas Borges';
To make it observable, you just need to add ".obs" to the end of it:
var name = 'Jonatas Borges'.obs;
And in the UI, when you want to show that value and update the screen whenever the values changes, simply do this:
Obx(() => Text("${controller.name}"));
For more , refer: https://pub.dev/packages/get/example
wrap you UI with Obx builder like
appBar: AppBar(title: Obx(() => Text("Clicks: ${c.count}"))),
it will auto refresh the UI on value change
if you want to refresh the UI manually then use GetBuilder and when you need to update the UI just call controller.update();
As #Muhammad Sherad explained, your widget code should be wrapped with Obx() so that you widget can look for variable change and re-render the widget.

This expression has a type of 'void' so its value can't be used. Flutter

I'm trying to change my app from stateful widgets to provider. I put a function in the provider class and for some reason, it keeps giving me this error when I try calling the function. Below is the code for it. Thanks.
`onTap: Provider.of<Infomation>(context).onTap(),`
This is the function in the provider class
'void onTap(){
currentIndex = index;
notifyListeners();
}'
You're actually calling the function and assigning the result to the onTap property.
Either you do it like:
onTap: Provider.of<Infomation>(context).onTap
without calling it, which assigns the function reference to it, or just call it inside like:
onTap: () {
Provider.of<Infomation>(context).onTap();
}
Try also to just keep a single reference of the extracted provider, since in the case that you may need it the provider again, you're gonna have to do that long thing again. Try doing at the top of the build method like:
var infoProvider = Provider.of<Infomation>(context, listen: false);
Then use the methods off of the infoProvider instance like infoProvider.onTap() and such.

How can I listen to a String variable change and perform an action when there is a change?

Is it possible to execute a function, lets call it myFunction() if a variable _myString is changed, but have this happen on the fly?
What I have is a textfield with a controller
var _myString;
const TextField(
controller:_controller
)
Now elsewhere in my widget tree, I have a button that can change the value of _myString, in this case I'm changing '_myString' to 'Testing'
GestureDetector(
onTap:(){ _myString = 'Testing'; }
child: Text('Testing')
)
Now what I'm hoping to achieve is that when the value of _myString changes in any way, I can perform some action of my choosing. In this case, I want to edit the TextField using the _controller, but I don't only want to do that, but a few other things, so I think its better to listen to changes in that variable and then execute a function
void myFunction(){
///Do some stuff
}
I'm using riverpod for state management in my app, and I was thinking I could try to use it, but have no idea how to use it to watch a single variable, I'm more familiar with using it for entire widgets. Alternatively using riverpod for something like this might be overkill.
I just don't know how to approach this problem, so any guidance would be really appreciated!
I believe you could use a ValueNotifier for this, a value notifier is a class that holds some value and "notifies" its listeners when this value changes. It is a simpler version of ChangeNotifier:
ValueNotifier<String> _myString = ValueNotifier<String>('');
With the above, whenever you want to read or write the value, use the value getter/setter:
print(_myString.value);
_myString.value = 'some value';
Now, to listen to changes you should use the addListener method:
#override
initState() {
// update _controller with value whenever _myString changes
_myString.addListener(() => _controller.text = _myString.value);
// print value on change
_myString.addListener(() => print(_myString.value));
// do stuff
_myString.addListener(myFunction)
}

Can a state in flutter call itself?

I have a stateful widget, that gets updated with button press events, is it viable to call the state from inside the same state on button press event? The states are a lot in number. So I want to avoid initializing too many states.
Yes you can call setState((){}) inside a StatefulWidget. According to the documentation:
Notify the framework that the internal state of this object has changed.
That means that, if you want to update any value on your StatefulWidget, make sure to call it inside the setState((){}), like this: setState(() { _myState = newValue; });
From what I understood, you don't want to have too many calls to the setState((){}) function. Since you are using a button, you can do it like this:
FlatButton(
child: Text("Tap me!"),
onPressed: () => setState((){ tapped = true}),
),
The arrows are syntatic sugar that substitute the curly brackets. Useful when there is only one line of code.

onTap replace widget from another file flutter

Say my App() contains some widget and to make the code look pretty, i have created classes for child widgets. Now these child widgets contain onTap functions which are supposed to replace a widget on App(), so how do i approach this type of problem ?
Some code of what you're doing would be useful or where you're trying to "replace a widget".
If by replace you mean show a different widget in the place of another then you just use a boolean to decide which one to show. These are the steps I would follow to implement this.
Make the widget in your App() stateful and create a member variable boolean called showingOriginalWidget = true;
In your child widget classes take in a Function in the parameter called onSwapWidget.
In your onTap function in your child widget call onSwapWidget()
In your App() supply the widget that's performing this action with your Function to call back to
See below
childWidget(onSwapWidget: (){
setState((){
// toggle the original widget state
showingOriginalWidget = !showOriginalWidget;
});
});
Where you're showing your widgets add a condition so that you show either one depending on the value.
example
...
child: showingOriginalWidget ? originalWidget() : swappedOutWidget()
...
That should do the trick.