SJente
SJente

Reputation: 33

Flutter Back Button pressed

WillPopScope won't react on my android back button device, but it does on the flutter arrow back. Anybody got an idea how to fix this?

class DetailScreen extends StatelessWidget {
  final Property property;
  const DetailScreen(this.property);

return WillPopScope(
  onWillPop: () {
    _goToProjects(context);
  },
child: ScopedModelDescendant<PropertyScopedModel>(
  builder: (context, child, model) => Scaffold(

        backgroundColor: Color(0xff253138),
        body: CustomScrollView(
          slivers: <Widget>[
            SliverAppBar(
              leading: IconButton(icon: Icon(Icons.arrow_back), onPressed: () {_goToProjects(context);} ),
                pinned: true,
                floating: false,
                title: Text('Project titel')),
            SliverList(
                delegate: SliverChildListDelegate([
              Container(
                color: Color(0xff2f3e47),
                padding: const EdgeInsets.all(16),
                margin: const EdgeInsets.symmetric(vertical: 2.0),

                child: Column(

                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Row(
                      children: <Widget>[

                        Expanded(
                          child: Text(
                            "INTERN",
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                                color: Colors.green,
                                fontWeight: FontWeight.bold),
                          ),
                        ),
                        Chip(
                          backgroundColor: Colors.green.shade800,
                          labelStyle: TextStyle(color: Colors.white),
                          label: Text('In planning'),
                        )
                      ],
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Text(
                      "Project titel",
                      style: TextStyle(
                          color: Colors.white, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Text(
                      property.summary,
                      style: Theme.of(context).textTheme.body2,
                    ),
                  ],
                ),
              ),


              Container(
                color: Color(0xff2f3e47),
                margin: const EdgeInsets.symmetric(vertical: 2.0),
                padding: const EdgeInsets.all(8),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Align(
                      alignment: Alignment.center,
                      child: Container(
                          child: Text("",
                              style: TextStyle(
                                  color: Colors.blue, fontSize: 16))),
                    ),
                    ListTile(
                      leading: Icon(Icons.pin_drop, color: Colors.white),
                      title: Text("",
                          style: TextStyle(fontSize: 14)),
                      subtitle: Text("",
                          style: TextStyle(fontSize: 14)),
                      onTap: () {
                        _launchMaps();
                      },
                    ),
                    ListTile(
                      leading: Icon(Icons.local_phone, color: Colors.white),
                      title: Text('',
                          style:
                              TextStyle(color: Colors.blue, fontSize: 14)),
                      onTap: () => launch(""),
                    ),
                    ListTile(
                      leading: Icon(Icons.mail, color: Colors.white),
                      title: Text('',
                          style: TextStyle(fontSize: 14)),
                      onTap: () {
                        _launchMail();
                      },
                    ),
                    ListTile(
                      leading: Icon(Icons.web, color: Colors.white),
                      title: Text(
                        '',
                        style: TextStyle(color: Colors.blue, fontSize: 14),
                      ),
                      onTap: () {
                        _launchURL();
                      },
                    ),
                  ],
                ),
              ),
              Container(
                margin: const EdgeInsets.symmetric(vertical: 2.0),
              ),
              Container(
                color: Color(0xff2f3e47),
                margin: const EdgeInsets.symmetric(vertical: 0.0),
                padding: const EdgeInsets.all(12),

                child: Row(
                  children: <Widget>[
                    Text(
                      "Taken",
                      style: Theme.of(context)
                          .textTheme
                          .title
                          .copyWith(fontSize: 20.0),
                    ),
                  ],
                ),
              ),



              InkWell(
                onTap: () {
                  print('test');
                },
                child: Container(

                  color: Color(0xff2f3e47),
                    child: Row(
                      children: <Widget>[
                        Expanded(
                          child: Container(

                            padding: const EdgeInsets.all(12.0),
                            child: Column(
                              mainAxisAlignment:
                                  MainAxisAlignment.spaceEvenly,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Divider(height: 3, color: Color(0xff253138),),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: <Widget>[
                                    Text('', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold), ),
                                    Text('12-02-2019'),
                                  ],
                                ),

                                SizedBox(
                                  height: 10,
                                ),

                                Row(
                                  children: <Widget>[
                                    Expanded(
                                      child: Text(
                                        property.summary,
                                        style: Theme.of(context)
                                            .textTheme
                                            .body2,
                                      ),
                                    ),
                                    Chip(
                                      backgroundColor:
                                          Colors.green.shade800,
                                      label: Text('In planning'),
                                    )
                                  ],
                                ),
                                Divider(height: 3, color: Colors.red,),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                ),
              ),
              Container(
                margin: const EdgeInsets.symmetric(vertical: 2.0),
              ),
              Container(
                color: Color(0xff2f3e47),
                margin: const EdgeInsets.symmetric(vertical: 2.0),
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.only(bottom: 8.0),
                      child: Text(
                        "Lister",
                        style: Theme.of(context)
                            .textTheme
                            .title
                            .copyWith(fontSize: 20.0),
                      ),
                    ),
                    ListTile(
                      leading: Icon(Icons.account_circle),
                      title:
                          Text("${property?.listerName ?? "unavailable"}"),
                      subtitle: Text(
                          "${property?.datasourceName ?? "source unavailable"}"),
                    ),
                  ],
                ),
              ),
            ]))
          ],
        ),
        floatingActionButton: AnimatedFloatingActionButton(
            //Fab list
            fabButtons: <Widget>[float1(), float2(), float3()],
            colorStartAnimation: Color(0xff0f70b7),
            colorEndAnimation: Colors.red,
            animatedIconData: AnimatedIcons.menu_close //To principal button
            ),
      ),
),
);
  }
}

And he should react on this:

void _goToProjects(context) {
  print('test');
  Navigator.push(context, MaterialPageRoute(builder: (context) {
    return GetProjects();
  }));

}

Tried a lot of different things but nothing is working. I hope somebody got an idea on how to fix this problem and knows what i'm doing wrong.

Thanks in advance

Upvotes: 0

Views: 1232

Answers (1)

Yash Jain
Yash Jain

Reputation: 415

WillPopScope is a StatefulWidget widget. So convert your DetailScreen class into a StatefulWidget class.

Wherever you have property.summary changed it to widget.property.summary and try it Change this code in your project:

        class DetailScreen extends StatefulWidget {
      final Property property;
      DetailScreen({Key key, this.property}) : super(key: key);
      @override
      DetailScreenState createState() {
        return DetailScreenState();
      }
    }

    class DetailScreenState extends State<DetailScreen> {
      Future<bool> _goToProjects() {
        print('test');
        return Navigator.push(
            context, MaterialPageRoute(builder: (context) => GetProjects()));
      }


    return WillPopScope(
      onWillPop: _goToProjects,
      child: ScopedModelDescendant<PropertyScopedModel>(
            ...
                Text(
                      "Project titel",
                      style: TextStyle(
                          color: Colors.white, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Text(
                      widget.property.summary,
                      style: Theme.of(context).textTheme.body2,
                    ),
                ... code
                Row(
                  children: <Widget>[
                    Expanded(
                      child: Text(
                        widget.property.summary,
                        style: Theme.of(context)
                            .textTheme
                            .body2,
                      ),
                    ),
                    Chip(
                      backgroundColor:
                          Colors.green.shade800,
                      label: Text('In planning'),
                    )
                  ],
                ),
        ..rest of your code
        ),
      );
    }

Upvotes: 1

Related Questions