3

I need to get a context for my Navigator push, i have this Navigator on my function _navigate. I try something like _navigate(BuildContext context) but i got an error like "type (BuildContext) => dynamic is not a subtype of type() => void. It's the first context of Navigator.push i don't know how to get it.

import 'package:flutter/material.dart'; import 'package:pandora_etna/assistance.dart'; import 'dart:math'; import 'package:vector_math/vector_math.dart' show radians; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( backgroundColor: Color(0xFFB3E5FC), body: SizedBox.expand(child: RadialMenu())), ); } } class RadialMenu extends StatefulWidget { createState() => _RadialMenuState(); } class _RadialMenuState extends State<RadialMenu> with SingleTickerProviderStateMixin { AnimationController controller; void initState() { super.initState(); controller = AnimationController(duration: Duration(milliseconds: 900), vsync: this); } Widget build(BuildContext context) { return RadialAnimation(controller: controller); } } class RadialAnimation extends StatelessWidget { RadialAnimation({Key key, this.controller}) : scale = Tween<double>( begin: 1.5, end: 0.0, ).animate( CurvedAnimation(parent: controller, curve: Curves.fastOutSlowIn), ), translation = Tween<double>( begin: 0.0, end: 100.0, ).animate( CurvedAnimation(parent: controller, curve: Curves.linear), ), rotation = Tween<double>(begin: 0.0, end: 360.0).animate( CurvedAnimation( parent: controller, curve: Interval( 0.0, 0.7, curve: Curves.decelerate, )), ), super(key: key); final AnimationController controller; final Animation<double> scale; final Animation<double> translation; final Animation<double> rotation; build(context) { return AnimatedBuilder( animation: controller, builder: (context, builder) { return Transform.rotate( angle: radians(rotation.value), child: Stack(alignment: Alignment.center, children: [ _buildButton(0, _close, color: Color(0xFF29B6F6), icon: FontAwesomeIcons.chartBar), _buildButton(60, _close, color: Color(0xFF29B6F6), icon: FontAwesomeIcons.clipboard), _buildButton(120, _close, color: Color(0xFF29B6F6), icon: FontAwesomeIcons.chartBar), _buildButton(180, _close, color: Color(0xFF29B6F6), icon: FontAwesomeIcons.book), _buildButton(240, _close, color: Color(0xFF29B6F6), icon: FontAwesomeIcons.arrowsAltV), _buildButton(300, _navigate, color: Color(0xFF29B6F6), icon: FontAwesomeIcons.phoneAlt), _buildButton(360, _close, color: Color(0xFF29B6F6), icon: FontAwesomeIcons.compressAlt), Transform.scale( scale: scale.value - 1.5, child: FloatingActionButton( child: Icon(FontAwesomeIcons.timesCircle), onPressed: _close, backgroundColor: Color(0xFF29B6F6)), ), Transform.scale( scale: scale.value, child: FloatingActionButton( child: Icon(FontAwesomeIcons.solidDotCircle), onPressed: _open, backgroundColor: Color(0xFF29B6F6)), ), ])); }); } _buildButton(double angle, Function callback, {Color color, IconData icon}) { final double rad = radians(angle); return Transform( transform: Matrix4.identity() ..translate( (translation.value) * cos(rad), (translation.value) * sin(rad)), child: FloatingActionButton( child: Icon(icon), backgroundColor: color, onPressed: callback)); } _open() { controller.forward(); } _close() { controller.reverse(); } _navigate(BuildContext context) { Navigator.push( context, new MaterialPageRoute(builder: (context) => Assistance())); } } 

5 Answers 5

2

I think it is because you lose your context argument when calling _buildButton.

/// By writing your Function parameter like this you will pass the /// BuildContext parameter needed. _buildButton( 300, () => _navigate(context), color: Color(0xFF29B6F6), icon: FontAwesomeIcons.phoneAlt ), 
Sign up to request clarification or add additional context in comments.

2 Comments

It's seems to work, but i got an error "There are multiple heroes that share the same tag within a subtree"
This has nothing to do with your navigation.
1

You need to pass BuildContext to the _navigate function when you reference it. You could do this with an anonymous function. _buildButton(300,(context) => _navigate(context), ...

3 Comments

It's seems to work, but i got an error "There are multiple heroes that share the same tag within a subtree"
I have never got that error, but judging from stackoverflow.com/questions/51125024/… it may be something to do with the fact you have two FloatingActionButtons on the same screen. If you only need one of the stack children at once maybe try IndexStack instead.
Yeah, i was looking on the internet for an answer. I fix it with some heroTag. It's so strange Flutter , it's seems like Android Studio is more easier haha. Thanks a lot for the help !
0

It doesn't work because you didn't give the context parameter of the function's method. I guess so

 _buildButton(300, _navigate(context), color: Color(0xFF29B6F6), icon: FontAwesomeIcons.phoneAlt), 

1 Comment

Thanks you for your answer !
0

Try out below example for more idea:-

 _buildButton(300, _navigate(context), // Added Context here,It will contains context of build function color: Color(0xFF29B6F6), icon: FontAwesomeIcons.phoneAlt), 
 _navigate(BuildContext context) { Navigator.push( context, new MaterialPageRoute(builder: (context) => Assistance())); } 

1 Comment

Thanks you for your answer !
0

When calling your function _buildButton you must wrap _navigate function into another function, since it need your current context to work
Try this

_buildButton(300, () { _navigate(context); }, color: Color(0xFF29B6F6), icon: FontAwesomeIcons.phoneAlt), 

1 Comment

Thanks you for your answer !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.