Closed pishguy closed 5 years ago
@MahdiPishguy check how I pass the title and make instead of String a Map<String, dynamic>
@amreniouinnovent my mean is passing parameter between fragments
@MahdiPishguy same like the title it could be added to the event in FragmentManager().addFragment(event.routeName); could update data too
if (event is FragmentNavigateEvent) {
if (!FragmentManager().isExist(event.routeName)) {
FragmentManager().addFragment(event.routeName);
}
final int currentIndex =
FragmentManager().navigateToName(event.routeName);
print('bloc currentIndex:$currentIndex');
yield FragmentCurrentState(currentIndex);
@amreniouinnovent i'm not sure for this implementation, because i have more parameter, not only single parameter, whats your idea?
@MahdiPishguy here you will add a parameter like a title in the fragment interface and fragment 1 https://github.com/amorenew/FlutterBlocFragment/blob/master/lib/fragments/fragment1.dart and same like int navigateToName(String fragmentName) { } you can make updateData(String fragmentName,dynamic data)
more than value could be Map<String,dynamic>
@amreniouinnovent thanks, i check that
@MahdiPishguy same like the title it could be added to the event in FragmentManager().addFragment(event.routeName); could update data too
if (event is FragmentNavigateEvent) { if (!FragmentManager().isExist(event.routeName)) { FragmentManager().addFragment(event.routeName); } final int currentIndex = FragmentManager().navigateToName(event.routeName); print('bloc currentIndex:$currentIndex'); yield FragmentCurrentState(currentIndex);
i don't understood how can i pass value
@MahdiPishguy in navigateToName there is a for loop once you catch the matched fragment pass the value to it
(value.child as Fragment).setData(value );
_fragments.asMap().forEach((int index, Widget value) {
if (value is Fragment && value.getRouteName() == fragmentName) {
fragmentIndex = index;
} else if (value is BlocProvider &&
value.child is Fragment &&
(value.child as Fragment).getRouteName() == fragmentName) {
fragmentIndex = index;
} else if (value is MultiBlocProvider &&
value.child is Fragment &&
(value.child as Fragment).getRouteName() == fragmentName) {
fragmentIndex = index;
}
});
@amreniouinnovent your mean is i should use switch()
statement?
@MahdiPishguy No. just in navigateToName pass your data as a new parameter then inside the existing for loop use (value.child as Fragment).setData(value );
so Navigation Event have new data parameter then navigateToName passing the data to the Fragment
don't forget to add a data field to Fragment interface.
@amreniouinnovent i'm sorry i don't know how can i implementing that
@MahdiPishguy I am so busy currently try to use singleton class if it is urgent
@amreniouinnovent i get null
i'm not sure my code is correct:
https://github.com/MahdiPishguy/FlutterBlocFragmentTest
int navigateToName(String fragmentName) {
int fragmentIndex = -1;
print('manager navigateToName:$fragmentIndex');
_fragments.asMap().forEach((int index, Widget value) {
if (value is Fragment && value.getRouteName() == fragmentName) {
fragmentIndex = index;
} else if (value is BlocProvider && value.child is Fragment && (value.child as Fragment).getRouteName() == fragmentName) {
fragmentIndex = index;
(value.child as Fragment).setData((value.child as Fragment).getParameter());
} else if (value is MultiBlocProvider && value.child is Fragment && (value.child as Fragment).getRouteName() == fragmentName) {
fragmentIndex = index;
(value.child as Fragment).setData((value.child as Fragment).getParameter());
}
});
print('manager navigateToName:$fragmentIndex');
_backstack.add(fragmentIndex);
_currentIndex = fragmentIndex;
return fragmentIndex;
}
Fragment1
class Fragment1 extends StatefulWidget implements Fragment {
String myname;
@override
State<Fragment1> createState() => _Fragment1();
...
@override
String getParameter() {
return 'aaaaaaaa';
}
@override
void setData(String value) {
myname = value;
}
}
Fragment
abstract class Fragment extends Widget{
String getRouteName();
String getTitle();
String getParameter();
void setData(String value) {}
}
@MahdiPishguy could you pass a fixe value variable if it works then add parameter to navigateToName(String fragmentName)
@amreniouinnovent
i get null again if i understand whats your mean
(value.child as Fragment).setData('11111');
@MahdiPishguy if I got time I will add this implementation
@amreniouinnovent can you got free time to help me today?
@MahdiPishguy I have to deliver this week a new release so it too hard to support this week
@MahdiPishguy you can use a singleton class for now
class GlobalData {
factory GlobalData() => _instance ??= new GlobalData._();
GlobalData._();
//One instance, needs factory
static GlobalData _instance;
bool isDriverTablet = false;
}
Usage:
GlobalData().isDriverTablet
@amreniouinnovent
@MahdiPishguy you can use a singleton class for now
class GlobalData { factory GlobalData() => _instance ??= new GlobalData._(); GlobalData._(); //One instance, needs factory static GlobalData _instance; bool isDriverTablet = false; }
Usage:
GlobalData().isDriverTablet
I get stackoverflow
error
@MahdiPishguy StackOverflow is related to your logic you call nested calls over and over
@amreniouinnovent maybe, because i use that whenever i need to pass data :(
@amreniouinnovent hi,
can you got free time to help me how can i implementing this feature?
thanks in advance
@MahdiPishguy check the latest commit I added an example for passing the parameter confirm if it works
@amreniouinnovent thank you very much
@MahdiPishguy You are welcome
i want to pass some parameters for fragments. with this implementation we can't and i want to know whats best solution about this issue