Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InkWell widgets require a Material widget ancestor

I am adding InkWell in Row as Widget but it is throwing me an error:

flutter: ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
flutter: The following assertion was thrown building InkWell(gestures: [tap], clipped to BoxShape.rectangle,
flutter: dirty, state: _InkResponseState<InkResponse>#0e6c5):
flutter: No Material widget found.
flutter: InkWell widgets require a Material widget ancestor.

Here is my code:

Container(
  color: Colors.red,
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      InkWell(
        onTap: (){
          //Forgot password Tapped
        },
      child: Text(Constants.forgotPassword),),
    ],
),
like image 341
Code Hunter Avatar asked Jun 20 '19 04:06

Code Hunter


3 Answers

InkWell Class will always work with a Material Class

Please try the code below.

Code: Wrap the InkWell Class with the Material Class

  Material(
          child: InkWell(
      onTap: (){
        //Forgot password Tapped
      },
    child: Text(Constants.forgotPassword),),
  ),

Thanks

like image 53
HaSnen Tai Avatar answered Oct 08 '22 19:10

HaSnen Tai


There are two ways to resolve the above issue.

  1. Replace InkWell with GestureDetector

     GestureDetector(
       onTap: () {
    
     },);
    
  2. Wrap InkWell with Material, as InkWell required Material Widget to wrap

     InkWell(
       onTap: () {
    
         },);
    

Note: To introduce a Material widget, you can either directly include one, or use a widget that contains Material itself, such as a Card, Dialog, Drawer, or Scaffold.

like image 34
Jitesh Mohite Avatar answered Oct 08 '22 19:10

Jitesh Mohite


Wrap the InkWell Class with Scaffold, then the issue will be gone.

like image 3
Haley Huynh Avatar answered Oct 08 '22 19:10

Haley Huynh