从另一个类小部件调用函数

时间:2020-03-14 17:12:17

标签: flutter dart

我有一个页面,其中包含AppBar

Widget build(BuildContext context) {
return Scaffold(
    appBar: AppBar(
      elevation: 0.0,
      title: Text('Admin panel'),
      leading: IconButton(
        icon: Icon(Icons.menu),
        onPressed: menuButtonPressed <<<<<<<<<?
      ),
    ),
    body: BlocProvider<NavigationBloc>(
      create: (context) => NavigationBloc(),
      child: Stack(
        children: <Widget>[
          BlocBuilder<NavigationBloc, NavigationStates>(
            builder: (context, navigationState) {
              return navigationState as Widget;
            },
          ),
          TestSideBar(),

我有自定义的侧边栏

    class _TestMenu extends State<TestMenu> with SingleTickerProviderStateMixin {
  AnimationController _animation;

  @override
  void initState() {
    _animation = AnimationController(
      value: 1,
      vsync: this,
      duration: drawerAnimationDuration,
    );

    super.initState();
  }

  @override
  void dispose() {
    _animation.dispose();
    super.dispose();
  }

  void menuButtonPressed() {
    if (_animation.isAnimating) return;
    _animation.isCompleted ? _animation.reverse() : _animation.forward();
  }

  @override
  Widget build(BuildContext context) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: Offset(-1, 0),
        end: Offset.zero,
      ).animate(_animation),
      child: Row(
        children: <Widget>[
          Container(
            width: drawerWidth,
            decoration: BoxDecoration(
              border: Border(right: BorderSide(color: Colors.grey[300])),
            ),

功能menuButtonPressed关闭并打开侧边栏。我的问题是来自menuButtonPressed中TestSideBar页面的调用函数AppBar。有可能吗?

0 个答案:

没有答案