颤振后退按钮按下

时间:2019-03-05 12:00:57

标签: flutter

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

}

尝试了许多不同的方法,但是没有任何效果。我希望有人对如何解决此问题有所了解,并且知道我在做什么错。

预先感谢

1 个答案:

答案 0 :(得分:1)

WillPopScope是一个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
        ),
      );
    }