使用通用AppBar的Flutter导航

问题描述 投票:0回答:1

我目前正在编写Flutter应用。我有一个AdvancedAppBar类,它扩展了AppBar,并被用作大多数屏幕的通用化AppBar。这是一个单独的文件。 AdvancedAppBar具有FlatButton和PopUpMenu作为子级。通过按下这些按钮,我想导航到另一个屏幕。目前,我使用Navigator类在屏幕之间进行导航,但据我所知,它假定使用BuildContext,但我不知道如何或是否有可能为AppBar定义BuildContext。

如何实现这种导航?

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue
      ),
      initialRoute: '/',
      routes: {
        '/': (context) => HomeScreen(),
        '/notifications': (context) => NotificationScreen(),
      },
    );
  }
}


class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AdvancedAppBar(),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new MaterialButton(
              height: 40.0,
              minWidth: 300.0,
              color: Colors.blue,
              textColor: Colors.white,
              child: new Text("Nachrichten", textScaleFactor: 2.0,),
              onPressed: (){
                Navigator.pushNamed(context, '/notifications');
                },
            )
          ],
        ),
      ),
    );
  }
}


class AdvancedAppBar extends AppBar{


  AdvancedAppBar({Key key}) : super(
        key: key,
        title:
        Container(child: ConstrainedBox(
        constraints: BoxConstraints.expand(),
          child: FlatButton(
          onPressed: () {
            // here I would like to navigate to another screen
          },
         padding: EdgeInsets.all(0.0),
         child: Image.asset('assets/images/Logo.png')
         )
         )
         ),
        actions: <Widget>[
          PopupMenuButton<PopUpStates>(
  onSelected: (PopUpStates result) {
    switch (result) {
      case PopUpStates.settings: {
      // here I would like to navigate to another screen
      } 
        break;
      case PopUpStates.logout: {
      // here I would like to navigate to another screen
      } 
      break;
      default:
    }},
  itemBuilder: (BuildContext context) => <PopupMenuEntry<PopUpStates>>[
    const PopupMenuItem<PopUpStates>(
      value: PopUpStates.settings,
      child: Text('Einstellungen'),
    ),
    const PopupMenuItem<PopUpStates>(
      value: PopUpStates.logout,
      child: Text('Ausloggen'),
    ),
  ],
)
        ],
        automaticallyImplyLeading: false
  );

}


flutter navigator appbar
1个回答
0
投票

让我们修改AdvancedAppBar的构造函数,使其看起来像这样,以便您可以传递函数

AdvancedAppBar({Key key, Function settings, Function logout})

现在我们可以在onSelected参数中使用这些参数,现在看起来像这样

onSelected: (PopUpStates result) {
    switch (result) {
      case PopUpStates.settings: {
        settings();
      } 
        break;
      case PopUpStates.logout: {
        logout();
      } 
      break;
      default:
    }},

最后,让我们从主屏幕传递导航器功能

appBar: new AdvancedAppBar(
  settings: () => Navigator.pushNamed(context, '/settings'),
  logout: () => Navigator.pushNamed(context, '/logout'),
),
© www.soinside.com 2019 - 2024. All rights reserved.