我通过教程制作了一个自己的带有渐变颜色的应用栏。但是现在我不能使用按钮导航到我的设置视图。该按钮需要上下文。您能帮我解决这个问题吗?谢谢!
class MyAppBar extends AppBar {
MyAppBar({Key key, Widget title})
: super(
key: key,
title: title,
centerTitle: true,
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
Color.fromRGBO(255, 120, 84, 1),
Color.fromRGBO(236, 10, 131, 1)
])
)),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.notifications),
onPressed: ()=> print("tap"),
),
new IconButton(
icon: new Icon(Icons.settings),
onPressed: () => Navigator.pushReplacementNamed(context, Settings.id),
)
]);
}
您需要扩展一个StatelessWidget以获取您的BuildContext,然后实现PreferredSizeWidget,因为AppBar本身实现了它。
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
final String screenTitle;
MyAppBar({@required this.screenTitle});
@override
Widget build(BuildContext context) {
return AppBar(
title: Text(screenTitle),
actions: // Whatever you need
);
}
@override
Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
您还需要重写get preferredSize并指定一个高度。在此示例中,我将Flutter已经指定的常量设为56.0作为AppBar的工具栏组件。