Flutter 按下“颤振后退”按钮

Flutter 按下“颤振后退”按钮,flutter,Flutter,WillPopScope不会在我的android后退按钮设备上做出反应,但它会在颤振箭头上做出反应。有人知道怎么解决这个问题吗 class DetailScreen extends StatelessWidget { final Property property; const DetailScreen(this.property); return WillPopScope( onWillPop: () { _goToProjects(context); }, chil

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
            ),
      ),
),
);
  }
}
尝试了很多不同的方法,但都没有效果。我希望有人知道如何解决这个问题,并知道我做错了什么


提前感谢

Willposcope是一个StatefulWidget小部件。因此,将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
        ),
      );
    }

你在测试哪部手机?你也试过其他手机吗?@CopsOnRoad我试过我的安卓设备三星s8,我也试过NexusX5虚拟设备。两个都不起作用。我猜这是因为它周围的范围模型。但是我需要在我的代码中使用它来让我的东西正常工作;const detailscreen this.property;Detailscreen必须有一个方法体,因为DetailScreenState不是抽象的。解决了上面的评论,但仍然没有解决我的问题。其他人有主意了吗?我已经更新了上面的代码,如果这行得通或者不行,试试看DetailScreen上的键不起作用,只是const现在起作用了。我遇到的问题是ScopedModelSecondant必须是一个返回值。你对此有什么想法吗?还有构建器:上下文、子对象、模型{必须在其中。
        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
        ),
      );
    }