Flutter Drawer在屏幕中自定义AppBar

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

我是新手,我不知道如何构建我的新应用程序。

我有一个抽屉显示不同的屏幕(在Android开发中作为片段样式),我想为每个屏幕更改AppBar(添加按钮甚至将应用栏更改为sliverAppBar),但我不知道如何实现这一点。

class Main extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'My App',
      theme: new ThemeData(
        primarySwatch: Colors.teal,
      ),
      home: MyDrawer(),
    );
  }
}

在那个抽屉里:

return Scaffold(
  appBar: AppBar(
    title: Text(widget.appDrawerItems[_selectedDrawerIndex].title),
  ),
  drawer: new Drawer(
    child: new ListView(
      children: <Widget>[            
        new Column(children: appDrawerOptions),
        new Divider(),
        new Column(children: configurationDrawerOptions)
      ],
    ),
  ),
  body: _getDrawerItemWidget(_selectedDrawerIndex),
);

你能指出我正确的方向吗?

flutter navigation-drawer drawer
2个回答
0
投票

我不确定我是否理解你的问题。如果您想在按抽屉项目时打开不同的屏幕,可以使用ListTile并使用onTap属性,您可以导航到您的屏幕(使用Navigator)。

您可以使用与片段对应的tabcontroller。请参阅this


0
投票

我假设您在Navigator.push()中使用Navigator.pushNamed()Drawer推送屏幕,并且您推送的新屏幕是Widgets。如果这些新的屏幕是Scaffolds,而不是Columns,你可以为它们中的每一个定义appBar。这是一个例子:

在你的抽屉里,无论你在哪里打电话给Navigator.pushNamed()

ListTile(
 title: DrawerText(text: 'Second Screen'),
 onTap: () {
   Navigator.pushNamed(context, 'my_second_screen');
 },
),

在你的main

void main() {
  runApp(MaterialApp(
    home: MyHomeScreen(title: 'MyHomeScreen'),
    routes: <String, WidgetBuilder>{
      // define the routes
      'my_second_screen': (BuildContext context) => MySecondScreen(),
    },
  ));
}

您要导航到的屏幕MySecondScreen看起来像:

class MySecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // This is your custom app bar specific for this screen.
      ),
      body: SecondScreenBody(),
      drawer: MyDrawer(),
    );
  }
}

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.