FlatButton onPressed在定位小部件中不起作用

时间:2019-09-13 07:08:14

标签: flutter dart flutter-layout

每当我使用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,
                                     ),
                                   ),
                                 ),
                               ),
                             ],
                           )),

3 个答案:

答案 0 :(得分:1)

更改此行:

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

对此:

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

答案 1 :(得分:0)

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

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

希望这会有所帮助。

答案 2 :(得分:-1)

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