在TabbarView中使用animateTo函数时OnPressed函数异常

时间:2019-05-29 19:45:31

标签: flutter tabcontrol tabbar

我正在尝试为我的项目编写代码,该代码使用带有4个标签的TabBarView并具有全局密钥,以便可以在应用程序中的任何位置使用Tab控制器, 但是,当我尝试使用凸起按钮的onpressed函数调用animateTo函数时,它会按照以下方式保持抛出错误:

已完成此操作//尝试在与控制器相同的文件中创建函数以更改视图

class MyTabsState extends State<MyTabs> with SingleTickerProviderStateMixin {

  static final homePageKey=GlobalKey<MyTabsState>();
  TabController tabcontroller;

  @override
  void initState() {
    super.initState();
    tabcontroller = new TabController(vsync: this, length: 4);
  }

  @override
  void dispose() {
    super.dispose();
    tabcontroller.dispose();

  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      key: homePageKey,
      bottomNavigationBar: new Material(
          color: mainUiColor,
          child: new TabBar(
              controller: tabcontroller,
              unselectedLabelColor: Colors.grey,
              labelColor: Colors.redAccent,
              tabs: <Tab>[
                new Tab(icon: new Icon(Icons.home)),
                new Tab(icon: new Icon(Icons.photo_album)),
                new Tab(icon: new Icon(Icons.arrow_back)),
                new Tab(icon: new Icon(Icons.favorite)),
              ])),
      body: new TabBarView(controller: tabcontroller, children: <Widget>[
        new First(),
        new PhotoGallery(),
        new Third(),
        new SocialMedia(),
      ]),

    );
  }
  void changePage(){

     tabcontroller.animateTo(2);
  }
}
I/flutter ( 4993): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ I/flutter ( 4993): The following NoSuchMethodError was thrown while handling a gesture: I/flutter ( 4993): The method 'changePage' was called on null. I/flutter ( 4993): Receiver: null I/flutter ( 4993): Tried calling: changePage() I/flutter ( 4993): I/flutter ( 4993): When the exception was thrown, this was the stack: I/flutter ( 4993): #0    Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5) I/flutter ( 4993): #1      _PhotoGalleryState.build.<anonymous closure> (package:myproject/screenTabs/SecondTab.dart:97:52) I/flutter ( 4993):
#2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:511:14) I/flutter ( 4993):
#3      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:566:30) I/flutter ( 4993):
#4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:166:24) I/flutter ( 4993): #5      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:240:9) I/flutter ( 4993): #6    TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:211:7) I/flutter ( 4993): #7    GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27) I/flutter ( 4993): #8
_WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:225:20) I/flutter ( 4993):
#9      _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:199:22) I/flutter ( 4993):
#10     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7) I/flutter ( 4993):
#11     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7) I/flutter ( 4993):
#12     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7) I/flutter ( 4993):
#16     _invoke1 (dart:ui/hooks.dart:233:10) I/flutter ( 4993): #17     _dispatchPointerDataPacket (dart:ui/hooks.dart:154:5) I/flutter ( 4993): (elided 3 frames from package dart:async) I/flutter ( 4993): I/flutter ( 4993): Handler: onTap I/flutter ( 4993): Recognizer: I/flutter ( 4993):   TapGestureRecognizer#bfc40(debugOwner: GestureDetector, state: ready, won arena, finalPosition: I/flutter ( 4993):   Offset(126.9, 667.6), sent tap down)

1 个答案:

答案 0 :(得分:0)

如果我是一个博彩人,我敢打赌,您可能正在尝试在tabBarView中列出的页面之一上调用changePage()。全局密钥并不能真正使该项目在应用程序中的任何地方可用,您必须使用继承的窗口小部件将其存储在窗口小部件树中的较高位置,或将其作为属性传递给其子级才能实际使用。 。您可能要考虑研究的内容是CupertinoTabScaffold。然后,您可以将其包装在主视图周围,并在那里使用它,并使它显示在整个应用程序中。这可能比使用当前设置容易一些。