草庐IT

flutter 后退按钮按下

coder 2023-07-24 原文

WillPopScope 不会在我的 android 后退按钮设备上使用react,但它会在 flutter 箭头返回上使用react。有人知道如何解决这个问题吗?

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

他应该对此使用react:

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

}

尝试了很多不同的东西,但没有任何效果。我希望有人知道如何解决这个问题并且知道我做错了什么。

提前致谢

最佳答案

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

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

有关flutter 后退按钮按下的更多相关文章

  1. ruby-on-rails - Rails 单选按钮 - 模型中多列的一种选择 - 2

    我希望用户从一个模型的三个选项中选择一个。即我有一个模型视频,可以被评为正面/负面/未知目前我有三列bool值(pos/neg/unknown)。这是处理这种情况的最佳方式吗?为此,表单应该是什么样的?目前我有类似的东西但显然它允许多项选择,而我试图将它限制为只有一个..怎么办? 最佳答案 如果要使用字符串列,让我们说rating。然后在你的表单中:#...#...它只允许一个选择编辑完全相同但使用radio_button_tag: 关于ruby-on-rails-Rails单选按钮-模

  2. ruby-on-rails - 如何从按钮或链接单击的 View 调用 Rails 方法 - 2

    基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub

  3. ruby-on-rails - 如何在 Rails 中添加禁用的提交按钮 - 2

    我在ruby​​表单中有一个提交按钮f.submitbtn_text,class:"btnbtn-onemgt12mgb12",id:"btn_id"我想在不使用任何javascript的情况下通过ruby​​禁用此按钮 最佳答案 添加disabled:true选项。f.submitbtn_text,class:"btnbtn-onemgt12mgb12",id:"btn_id",disabled:true 关于ruby-on-rails-如何在Rails中添加禁用的提交按钮,我们在St

  4. ruby-on-rails - 从 ActiveAdmin has_many 表单助手中删除 "Add new"按钮 - 2

    我在事件管理员编辑页面中有嵌套资源,但我只想允许管理员编辑现有资源的内容,而不是添加新的嵌套资源。我的代码看起来像这样:formdo|f|f.inputsdof.input:authorf.input:contentf.has_many:commentsdo|comment_form|comment_form.input:contentcomment_form.input:_destroy,as::boolean,required:false,label:'Remove'endendf.actionsend但它在输入下添加了“添加新评论”按钮。我怎样才能禁用它,并只为主窗体保留f.ac

  5. ruby - 按 Enter 键而不是用鞋子单击按钮(Ruby) - 2

    正如标题所暗示的那样,我只是在寻找一种无需单击即可按下Shoes中的按钮的方法。我已经搜索了论坛,但不幸的是找不到任何东西。谢谢 最佳答案 这在Windows下的红色鞋子中不起作用,因为Windows控件窃取了所有事件。不过,我设法在window下穿着绿鞋工作。举个例子['green_shoes'].each(&method(:require))Shoes.app{e=edit_linebutton("Clickme!"){alert("Youclickedme.")}keypress{|k|alert("YoupressedEnt

  6. ruby - 如何使用 Mechanize 保存通过单击按钮下载的文件 - 2

    我正在尝试使用Mechanize来模拟点击网页上的按钮,然后会在浏览器中启动文件下载。这是我的代码片段form=page.forms.first#=>Mechanize::Formform=agent.page.form_with(:name=>"aspnetForm")button=form.button_with(:value=>"GPXfile")ppbuttonagent.submit(form,button)pp按钮的输出是这样显示的,这意味着它是右键:#但是在发出“agent.submit(form,button)”之后,我怎样才能让Mechanize检索单击该按钮时将发送

  7. ruby-on-rails - 将 Bootstrap 图标添加到 Ruby on Rails 中的按钮 - 2

    我想在RubyonRails中为按钮添加图标。目前我尝试了以下方法:"),{:controller=>'events',:action=>"upvoteEvent",:method=>"put",:id=>@event.id,:uid=>current_user.id},{:class=>"btnbtn-success"}%>生成以下html:"/>我遵循了此处发布的解决方案:https://stackoverflow.com/a/10468935/1385324,但这对我不起作用。我还测试了BootstrapCSS是否正常工作,只需在网站的其他任何地方插入一个图标即可。感谢您的帮助!

  8. Flutter 环境变量配置和flutter doctor中的错误解决 - 2

    一、环境变量右键点击我的电脑-属性:然后找到环境变量 1.Android的SDK不在C盘的话需要额外配这个到用户环境变量:ANDROID_HOMED:\AndroidSDK2.然后在系统变量:Path中添加一条这样的值        D:\Flutter\flutter\bin             这个值写flutter包解压的实际地址即可 3.在系统变量中添加两个镜像变量:        变量名:FLUTTER_STORAGE_BASE_URL      变量值:https://storage.flutter-io.cn        变量名:PUB_HOSTED_URL      变量

  9. ruby-on-rails - 在 Facebook、转推等上分享直接 Ruby gem 中的按钮? - 2

    注意http://techcrunch.com/2010/04/04/he-even-makes-coldplay-sound-fun/顶部的那些按钮在社交网络上分享网址?我想为我正在构建的网站做一些非常相似的事情。ShareThis提供了一个可以做同样事情的小部件,但它是品牌化的和外部的。我正在寻找纯Ruby解决方案。包含可包含在RailsApplicationHelper类中的模块的gem将是完美的。在我重新发明轮子之前,感谢您的建议!想象一下: 最佳答案 我能找到的最好的是:http://www.addthis.com/这里有

  10. ruby - 可以将操作按钮添加到 activeadmin 上的仪表板表吗? - 2

    我有这段代码可以在activeadmin仪表板上创建一个表:columnsdocolumndopanel"NewMentor'srequests"dotable_forUser.where(mentor_request:true)do|t|t.column("Id"){|user|user.id}t.column("Name"){|user|user.account.full_name}t.column("Email"){|user|user.account.email}t.column("Organization"){|user|user.organization.name}ende

随机推荐