Flutter Dart Bottomsheet error context = context

689

You are getting the error because context is not defined in _showSearch.

You should modify the function definition and add context as a parameter as follows:

void _showSearch(BuildContext context) {
...

And when you are calling, you must pass context of the widget from where you are calling it, as follows:

child: new IconButton(
  icon: new Icon(Icons.search),
  highlightColor: Colors.pink,
  onPressed: () {
    _showSearch(context); //here
  },
),
Share:
689
Henri Klein
Author by

Henri Klein

Updated on December 21, 2022

Comments

  • Henri Klein
    Henri Klein over 1 year

    I hope somebody can help me solve this error; I used this showModalBottomSheet Widget already, and just tried to implement the structure onto a new site. The error I get and just don't understand in the BottomSheet has something to do with " context = context, " . Do I have to iplement the void funktion somwhere else or should I create a new class that is extending the Header class? Can somebody please help me?

    class Home extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          resizeToAvoidBottomPadding: false,
          body: ListView(
            children: <Widget>[
              Header(),
              //SelectOption(),
              //Chats(),
              //MoodsDetector(),
              //NextUp(),
              //PostFeed(),
            ],
          ),
        );
      }
    }
    
    class Header extends StatelessWidget {
      const Header({
        Key key,
      }) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Container(
          padding: EdgeInsets.fromLTRB(25, 50, 50, 25),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    'Welcome back,',
                    style: TextStyle(color: secondColor, fontSize: 20),
                  ),
                  Text(
                    'Henri',
                    style: TextStyle(color: mainColor, fontSize: 30),
                  ),
                ],
              ),
              Container(
                width: 50,
                height: 50,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.white,
                  boxShadow: [
                    BoxShadow(color: secondColor.withOpacity(0.5), blurRadius: 20),
                  ],
                ),
                child: new IconButton(
                  icon: new Icon(Icons.search),
                  highlightColor: Colors.pink,
                  onPressed: _showSearch,
                ),
              ),
            ],
          ),
        );
      }
    
    
      void _showSearch(){
        showModalBottomSheet(
            backgroundColor: Colors.white,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.only(topRight: Radius.circular(30.0),topLeft: Radius.circular(30.0))
            ),
            isScrollControlled: true,
            isDismissible: true,
            context: context,
            builder: (builder) {
              return SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    Stack(
                      children: <Widget>[
                        SingleChildScrollView(
                          padding: const EdgeInsets.all(15.0),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Card(
                                elevation: 8.0,
                                shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(10.0)),
                                color: Colors.green,
                                child: ListTile(
                                  onTap: () {
                                    //open edit profile
                                  },
                                  title: Text(
                                    "Rate your Friendship",
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontWeight: FontWeight.w500,
                                    ),
                                  ),
                                  leading: CircleAvatar(
                                    backgroundImage: AssetImage("assets/images/HenriKlein.jpeg"),
                                  ),
                                  trailing: Icon(
                                    Icons.star,
                                    color: Colors.white,
                                  ),
                                ),
                              ),
                              const SizedBox(height:10.0),
                              Card(
                                elevation: 4.0,
                                margin: const EdgeInsets.fromLTRB(32.0, 8.0, 32.0, 16.0),
                                shape: RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(10.0)),
                                child: Column(
                                  children: <Widget>[
                                    ListTile(
                                      leading: Icon(
                                        Icons.people_outline,
                                        color: Colors.lightGreen,
                                      ),
                                      title: Text("Invite to event"),
                                      trailing: Icon(Icons.keyboard_arrow_right),
                                      onTap: () {
    
                                      },
                                    ),
                                    sizedBox,
                                    ListTile(
                                      leading: Icon(
                                        Icons.directions_run,
                                        color: Colors.lightGreen,
                                      ),
                                      title: Text("Challange Henri"),
                                      trailing: Icon(Icons.keyboard_arrow_right),
                                      onTap: () {},
                                    ),
                                    sizedBox,
                                    ListTile(
                                      leading: Icon(
                                        Icons.phone_iphone,
                                        color: Colors.lightGreen,
                                      ),
                                      title: Text("Text/Call Henri"),
                                      trailing: Icon(Icons.keyboard_arrow_right),
                                      onTap: () { },
                                    ),
    
                                    sizedBox,
                                    ListTile(
                                      leading: Icon(
                                        Icons.lock_outline,
                                        color: Colors.lightGreen,
                                      ),
                                      title: Text("Delete Friend"),
                                      trailing: Icon(Icons.keyboard_arrow_right),
                                      onTap: () {},
                                    ),
    
                                  ],
                                ),
                              ),
                              const SizedBox(height: 5.0),
    
                              Center(
                                child: Text(
                                  "Friens since 09/20/2019",
                                  style: TextStyle(
                                    fontSize: 15.0,
    
                                  ),
                                  textAlign: TextAlign.center,
                                ),
                              ),
                              SizedBox(height: 20.0),
                              Container(
                                height: 40.0,
                                child: GestureDetector(
                                  onTap: () {
                                  },
                                  child: Material(
                                    borderRadius: BorderRadius.circular(50.0),
                                    shadowColor: Colors.black,
                                    color: Colors.green,
                                    elevation: 7.0,
                                    child: Center(
                                      child: Text(
                                        '27 mutural friends',                                                                       //Login Button
                                        style: TextStyle(
                                            color: Colors.white,
                                            fontWeight: FontWeight.bold,
                                            fontFamily: 'Montserrat'),
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
    
                  ],
                ),
              );
            });
      }
    }
    
    
    • Christopher Moore
      Christopher Moore almost 4 years
      You're not passing context to your function.