我是新手,我不知道如何构建我的新应用程序。
我有一个抽屉显示不同的屏幕(在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),
);
你能指出我正确的方向吗?
我不确定我是否理解你的问题。如果您想在按抽屉项目时打开不同的屏幕,可以使用ListTile
并使用onTap
属性,您可以导航到您的屏幕(使用Navigator
)。
您可以使用与片段对应的tabcontroller
。请参阅this。
我假设您在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(),
);
}
}
希望这可以帮助。