【问题标题】:FlatButton onPressed Not Working In Positioned WidgetFlatButton onPressed 在定位的小部件中不起作用
【发布时间】:2019-09-13 07:08:14
【问题描述】:

每当我使用 FlatButton 时,onPressed 方法在定位的小部件中不起作用。

我正在设计一个屏幕,按钮始终位于屏幕底部,单击该按钮我们可以移动到其他屏幕,但在这里它不起作用,任何人都可以检查并帮助我。提前谢谢

           child: Stack(
               // height: MediaQuery.of(context).size.height,
               children: <Widget>[
                     Positioned(
                         left: 0,
                         bottom: 0,
                           // alignment: Alignment(0, 0),
                           child: Row(
                             mainAxisSize: MainAxisSize.max,
                             crossAxisAlignment: CrossAxisAlignment.end,
                             mainAxisAlignment: MainAxisAlignment.end,
                             children: <Widget>[
                               SizedBox(height: 115),
                               ButtonTheme(
                                 minWidth:
                                     MediaQuery.of(context).size.width / 2,
                                 height: 60.0,
                                 child: FlatButton.icon(
                                   icon: Icon(FontAwesomeIcons.fileInvoice),
                                   color: Colors.white,
                                   onPressed: () {
                                  MaterialPageRoute(builder: (context) => MainScreen());
                                   },
                                   label: Text(
                                     "My Orders",
                                     textAlign: TextAlign.center,
                                     style: TextStyle(
                                       // decoration: TextDecoration.underline,
                                       fontSize: 25,
                                       fontWeight: FontWeight.bold,
                                       color: Theme.of(context).accentColor,
                                     ),
                                   ),
                                 ),
                               ),
                               SizedBox(height: 115),
                               ButtonTheme(
                                 minWidth:
                                     MediaQuery.of(context).size.width / 2,
                                 height: 60.0,
                                 child: FlatButton.icon(
                                   icon: Icon(FontAwesomeIcons.dollarSign),
                                   color: Colors.white60,
                                   onPressed: () {
                                    Navigator.pushReplacementNamed(
                                       context, '/home');
                                   },
                                   label: Text(
                                     "Bean Balnace",
                                     textAlign: TextAlign.center,
                                     style: TextStyle(
                                       // decoration: TextDecoration.underline,
                                       fontWeight: FontWeight.bold,
                                       fontSize: 20,
                                       color: Theme.of(context).accentColor,
                                     ),
                                   ),
                                 ),
                               ),
                             ],
                           )),

【问题讨论】:

  • 您遇到任何错误?
  • 不,只是 onPressed 不起作用...
  • 两个按钮都不起作用吗?
  • 是的,两者都不起作用
  • 只要 MainScreen() 存在并且您已经设置了 /home 路由,它就应该可以工作。您确定调试控制台中没有任何错误吗?

标签: flutter dart flutter-layout


【解决方案1】:

改变这一行:

MaterialPageRoute(builder: (context) => MainScreen());

到这里:

Navigator.push(context, MaterialPageRoute(builder: (context) => MainScreen()));

【讨论】:

    【解决方案2】:

    我重新创建了您的案例,并且能够在点击两个按钮后导航到新屏幕。这是我尝试过的完整代码:

    /home 路由声明为:

    return MaterialApp(
          initialRoute: '/',
          routes: {
            '/home': (context) => NextScreen()
          },
          home: MyHomePage(),
        );
    

    那么,除了My Orders onPressed() 事件中的细微更改之外,几乎就是您共享的代码:

    return Scaffold(
          body: Container(
            child: Stack(
              // height: MediaQuery.of(context).size.height,
                children: <Widget>[
                Positioned(
                left: 0,
                bottom: 0,
                // alignment: Alignment(0, 0),
                child: Row(
                  mainAxisSize: MainAxisSize.max,
                  crossAxisAlignment: CrossAxisAlignment.end,
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    SizedBox(height: 115),
                    ButtonTheme(
                      minWidth:
                      MediaQuery.of(context).size.width / 2,
                      height: 60.0,
                      child: FlatButton.icon(
                        icon: Icon(Icons.forward),
                        color: Colors.white,
                        onPressed: () {
                          Navigator.push(context, MaterialPageRoute(builder: (context) => NextScreen()));
                        },
                        label: Text(
                          "My Orders",
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            // decoration: TextDecoration.underline,
                            fontSize: 25,
                            fontWeight: FontWeight.bold,
                            color: Theme.of(context).accentColor,
                          ),
                        ),
                      ),
                    ),
                    SizedBox(height: 115),
                    ButtonTheme(
                      minWidth:
                      MediaQuery.of(context).size.width / 2,
                      height: 60.0,
                      child: FlatButton.icon(
                        icon: Icon(Icons.business),
                        color: Colors.white60,
                        onPressed: () {
                          Navigator.pushReplacementNamed(
                              context, '/home');
                        },
                        label: Text(
                          "Bean Balnace",
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            // decoration: TextDecoration.underline,
                            fontWeight: FontWeight.bold,
                            fontSize: 20,
                            color: Theme.of(context).accentColor,
                          ),
                        ),
                      ),
                    ),
                  ],
                )),
            ]
          )
          )
        );
    

    NextScreen类:

    class NextScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // TODO: implement build
        return Scaffold(
          appBar: AppBar(
            title: Text('Test'),
          ),
        );
      }
    }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      尝试将您的小部件作为 Stack 小部件子级的最后一个小部件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-02
        • 2021-10-14
        • 1970-01-01
        • 2020-02-19
        • 1970-01-01
        • 2020-04-02
        • 2021-01-16
        • 1970-01-01
        相关资源
        最近更新 更多