Aashish K
Aashish K

Reputation: 103

How do I make the global type context variable in flutter?

I just figured out some problem when I tried to navigate from one page to the other in flutter. The Navigation.push method requires the context variable and I need reference to the context for navigating.

Widget navBox(Color aColor, double left, double top, Icon icon,String action, Route route) {
  return Positioned (
        left: left,
        top: top,
        child: InkWell(
          borderRadius: BorderRadius.circular(30.0),
            onTap: () {
              Navigator.push(context, route);
            },
            child: new Container(
            height: 220.0,
            width: 220.0,
            decoration: BoxDecoration(
              color: aColor,
              borderRadius: BorderRadius.circular(30.0),
            ),
            child: Padding(
              padding: EdgeInsets.all(40.0),
                child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  icon,
                  new Text(action, style: TextStyle(color: Colors.white, fontSize: 20.0, fontFamily: 'Oxygen')),
                ],
              ),
            ),
           ),
          ),
        );
}

I expected to navigate to the other page by the call of this function with proper route defined.

I got the error like:

The following NoSuchMethodError was thrown while handling a gesture

The method 'ancestorStateOfType' was called on null.

Upvotes: 1

Views: 5268

Answers (2)

Aashish K
Aashish K

Reputation: 103

Wrapping the InkWell with the new Builder or the Material widget solves the problem.

Here's the snippet on how I solved the problem:

child: new Container(
            height: 220.0,
            width: 220.0,
            decoration: BoxDecoration(
              color: aColor,
              borderRadius: BorderRadius.circular(30.0),
            ),
            child: new Builder(
              builder: (context) => InkWell(
                onTap: () {
                  Navigator.push(context,
                                 MaterialPageRoute(builder: (context) => GetData()),);
                },
                child: Padding(
                  padding: EdgeInsets.all(40.0),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        icon,
                        new Text(action, style: TextStyle(color: Colors.white, fontSize: 20.0, fontFamily: 'Oxygen')),
                ],
              ),
            ),
              ),
            ),
           ),

I am only listing the Container widget of the function. So rest of them could be figured out by yourself.

Upvotes: 0

Karol Lisiewicz
Karol Lisiewicz

Reputation: 674

Just add a context parameter to the method's signature. But also have in mind that methods shouldn't have so many parameters, because it makes them harder to use.

Upvotes: 2

Related Questions