代码:
class MenuWidget extends StatelessWidget {
final Function(String) onItemClick;
const MenuWidget({Key key, this.onItemClick}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: <Widget>[
sliderItem('Home', Icons.home,context),
sliderItem('Setting', Icons.settings,context),
sliderItem('LogOut', Icons.arrow_back_ios,context)
],
),
);
}
Widget sliderItem(String title, IconData icons) => ListTile(
title: Text(title,),
leading: Icon(icons),
onTap: () {
onItemClick(title);
});
}
变量title
会显示被点击的item的名字,比如“LogOut”或者“Setting”,我想修改onItemClick来导航到不同的页面,比如router.navigateTo(context, '/welcome')
,但是我不知道怎么做使用 onItemClick,谢谢!!!
答案 0 :(得分:0)
方法一
return Container(
child: Column(
children: <Widget>[
sliderItem('Home', Icons.home,context,(){
//todo
router.navigateTo(context, '/welcome');
}),
//Other items...
],
),
);
Widget sliderItem(String title, IconData icons, VoidCallback onClicked) => ListTile(
title: Text(title,),
leading: Icon(icons),
onTap: onClicked
);
方法 2(当您使用命名路由时!
return Container(
child: Column(
children: <Widget>[
sliderItem('Home', Icons.home,'welcome'),
//Other items...
],
),
);
Widget sliderItem(String title, IconData icons, String route) => ListTile(
title: Text(title,),
leading: Icon(icons),
onTap: (){
router.navigateTo(context, '/' + route);
}
);