Ive been trying to work this out for awhile now still getting a hang of flutter as its fairly new to me. Im trying to create an animated menu where once i click on the hamburger icon, it opens the menu and when choosing options within the menu, the screen changes. Im having an issue with passing the callback
function for each screen to change menuOpen
to true using setState
. Since I cannot add the callback function to the screen when adding it to the map, what would i need to do in order to send a function as a parameter to change the menuOpen
value to open or close the menu. When i add the call back function in the initialisation it says The instance member 'setState' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression
BuildPageStack builds the screen based on the current index of the map.
Widget buildPageStack(int place, List<Widget> pageList) {
final deviceWidth = MediaQuery.of(context).size.width;
return AnimatedPositioned(
duration: duration,
top: menuOpen ? deviceWidth * 0.10 : 0.0,
bottom: 0,
left: menuOpen ? deviceWidth * 0.40 - (place * 30) : 0.0,
right: menuOpen ? deviceWidth * -0.130 + (place * 30) : 0.0,
child: ScaleTransition(
scale: scaleAnimations[place],
child: GestureDetector(
onTap: () {
if (!mounted)
return;
else if (menuOpen) {
setState(() {
menuOpen = false;
animationController.reverse();
});
}
},
child: AbsorbPointer(
absorbing: menuOpen,
child: Stack(
children: [
Material(
animationDuration: duration,
borderRadius: BorderRadius.circular(menuOpen ? 20.0 : 0.0),
child: pageList[place]),
// Container(
// color: Theme.of(context).primaryColor.withOpacity(0.5),
// ),
],
),
),
),
),
);
}
initState method
void initState() {
super.initState();
animationController = AnimationController(vsync: this, duration: duration);
scaleAnimations = [
Tween<double>(begin: 1.0, end: 0.8).animate(animationController),
Tween<double>(begin: 1.0, end: 0.7).animate(animationController),
Tween<double>(begin: 1.0, end: 0.6).animate(animationController),
];
animationController.forward();
adminScreenSnapshot = adminpages.values.toList();
userScreenSnapshot = userpages.values.toList();
}
Map initialization and call, if the user is an admin it shows 3 screens, if user it shows 2
Map<int, Widget> adminpages = {
0: Main_Foster(menuCallback: (){
setState(() {
menuOpen = true;
animationController.forward();
});}),
1: ProfileScreen(menuCallback: (){
setState(() {
menuOpen = true;
animationController.forward();
});}),
2: CMS(menuCallback: (){
setState(() {
menuOpen = true;
animationController.forward();
});}),
};
Map<int, Widget> userpages = {
0: Main_Foster(menuCallback: (){
setState(() {
menuOpen = true;
animationController.forward();
});}),
1: ProfileScreen(menuCallback: () {
setState(() {
menuOpen = true;
animationController.forward();
});}),
};
List<Widget> adminScreenSnapshot;
List<Widget> userScreenSnapshot;
List<Widget> finalPageStack() {
List<Widget> returnStack = [];
returnStack.add(MenuScreen(
menuCallback: (selectedIndex) {
if (widget.isAdmin) {
setState(() {
adminScreenSnapshot = adminpages.values.toList();
final selectedWidget = adminScreenSnapshot.removeAt(selectedIndex);
adminScreenSnapshot.insert(0, selectedWidget);
});
} else {
setState(() {
userScreenSnapshot = userpages.values.toList();
final selectedWidget = userScreenSnapshot.removeAt(selectedIndex);
userScreenSnapshot.insert(0, selectedWidget);
});
}
},
));
if (widget.isAdmin) {
adminScreenSnapshot
.asMap()
.entries
.map((screen) => buildPageStack(screen.key, adminScreenSnapshot))
.toList()
.reversed
..forEach((screen) {
returnStack.add(screen);
});
} else {
userScreenSnapshot
.asMap()
.entries
.map((screen) => buildPageStack(screen.key, userScreenSnapshot))
.toList()
.reversed
..forEach((screen) {
returnStack.add(screen);
});
}
return returnStack;
}
FinalPageStack returns the required stack
List<Widget> finalPageStack() {
List<Widget> returnStack = [];
returnStack.add(MenuScreen(
menuCallback: (selectedIndex) {
if (widget.isAdmin) {
setState(() {
adminScreenSnapshot = adminpages.values.toList();
final selectedWidget = adminScreenSnapshot.removeAt(selectedIndex);
adminScreenSnapshot.insert(0, selectedWidget);
});
} else {
setState(() {
userScreenSnapshot = userpages.values.toList();
final selectedWidget = userScreenSnapshot.removeAt(selectedIndex);
userScreenSnapshot.insert(0, selectedWidget);
});
}
},
));
if (widget.isAdmin) {
adminScreenSnapshot
.asMap()
.entries
.map((screen) => buildPageStack(screen.key, adminScreenSnapshot))
.toList()
.reversed
..forEach((screen) {
returnStack.add(screen);
});
} else {
userScreenSnapshot
.asMap()
.entries
.map((screen) => buildPageStack(screen.key, userScreenSnapshot))
.toList()
.reversed
..forEach((screen) {
returnStack.add(screen);
});
}
return returnStack;
}