WillPopScope不会在我的Android后退按钮设备上做出反应,但它会在后面的颤振箭头上做出反应。有人知道怎么解决这个问题吗?

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
            ),
      ),
),
);
  }
}

他应该对此作出反应:
void _goToProjects(context) {
  print('test');
  Navigator.push(context, MaterialPageRoute(builder: (context) {
    return GetProjects();
  }));

}

尝试了很多不同的事情,但没有任何效果。我希望有人知道如何解决这个问题,知道我做错了什么。
提前谢谢

最佳答案

WillPopScope是一个状态小部件。因此,将detailscreen类转换为statefulwidget类。
无论您在何处使用property.summary,都会将其更改为widget.property.summary并尝试它。
在项目中更改此代码:

        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
        ),
      );
    }

关于flutter - 按下 flutter 后退按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55002396/

10-12 03:14