以下是否可以解决使用GetX控制器控制TabView的问题?
GetTicker
SingleTickerProviderMixin有一个Get版本,它实现了TickerProvider接口(使用来自Flutter SDK的相同Ticker类)。
它有一个引人注目的名称:
GetSingleTickerProviderStateMixin
(更新于21-12-20:SingleGetTickerProviderMixin
已经随着最新的GetX
被弃用。感谢评论者指出这一点。)
以下示例基本上来自TabController的Flutter文档。
从链接示例的StatefulWidget中,我将其内容移植到了一个GetxController(MyTabController
)中,并添加了SingleGetTickerProviderMixin
的混合:
class MyTabController extends GetxController with GetSingleTickerProviderStateMixin {
final List<Tab> myTabs = <Tab>[
Tab(text: 'LEFT'),
Tab(text: 'RIGHT'),
];
late TabController controller;
@override
void onInit() {
super.onInit();
controller = TabController(vsync: this, length: myTabs.length);
}
@override
void onClose() {
controller.dispose();
super.onClose();
}
}
在无状态小部件中使用MyTabController
:
class MyTabbedWidget extends StatelessWidget {
const MyTabbedWidget();
@override
Widget build(BuildContext context) {
final MyTabController _tabx = Get.put(MyTabController());
return Scaffold(
appBar: AppBar(
bottom: TabBar(
controller: _tabx.controller,
tabs: _tabx.myTabs,
),
),
body: TabBarView(
controller: _tabx.controller,
children: _tabx.myTabs.map((Tab tab) {
final String label = tab.text!;
return Center(
child: Text(
'This is the $label tab',
style: const TextStyle(fontSize: 36),
),
);
}).toList(),
),
);
}
}
以下是示例应用程序的其余部分,可直接复制并粘贴到Android Studio / VisualStudio Code中运行:
import 'package:flutter/material.dart';
import 'package:get/get.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('GetX Tab Example'),
),
body: Column(
children: [
Expanded(
flex: 1,
child: Container(
alignment: Alignment.center,
child: Text('Some random stuff'),
),
),
Expanded(
flex: 4,
child: MyTabbedWidget(),
)
],
),
);
}
}
class MyTabController extends GetxController with GetSingleTickerProviderStateMixin {
final List<Tab> myTabs = <Tab>[
Tab(text: 'LEFT'),
Tab(text: 'RIGHT'),
];
late TabController controller;
@override
void onInit() {
super.onInit();
controller = TabController(vsync: this, length: myTabs.length);
}
@override
void onClose() {
controller.dispose();
super.onClose();
}
}
class MyTabbedWidget extends StatelessWidget {
const MyTabbedWidget();
@override
Widget build(BuildContext context) {
final MyTabController _tabx = Get.put(MyTabController());
return Scaffold(
appBar: AppBar(
bottom: TabBar(
controller: _tabx.controller,
tabs: _tabx.myTabs,
),
),
body: TabBarView(
controller: _tabx.controller,
children: _tabx.myTabs.map((Tab tab) {
final String label = tab.text!;
return Center(
child: Text(
'This is the $label tab',
style: const TextStyle(fontSize: 36),
),
);
}).toList(),
),
);
}
}
SingleGetTickerProviderMixin
和GetSingleTickerProviderStateMixin
现在都已经被弃用,你可以从现在开始使用SingleGetTickerProviderMixin
。 - Jay Gadariya