Я пытаюсь написать код для моего проекта, который использует TabBarView с 4 вкладками и имеет глобальный ключ, чтобы я мог использовать контроллер вкладок в любом месте приложения,
однако, когда я пытался вызвать функцию 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)