Last active
September 12, 2024 18:00
-
-
Save Mastersam07/1e13c617a0a481572f371cdf9aeca8c7 to your computer and use it in GitHub Desktop.
RestorableTabController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A [RestorableProperty] that knows how to store and restore a | |
/// [TabController]. | |
/// | |
/// The [TabController] is accessible via the [value] getter. During | |
/// state restoration, the property will restore [TabController.index] | |
/// to the value it had when the restoration data it is getting restored from | |
/// was collected. | |
class RestorableTabController extends RestorableChangeNotifier<TabController> { | |
/// Creates a [RestorableTabController] to control the tab index of | |
/// [TabBar]. | |
/// | |
/// The `initialIndex` defaults to zero. The value must be greater than or | |
/// equal to zero, and less than the total number of tabs. | |
RestorableTabController({ | |
int initialIndex = 0, | |
int length = 2, | |
required TickerProvider vsync, | |
}) : assert(length >= 0), | |
assert(initialIndex >= 0 && (length == 0 || initialIndex < length)), | |
_vsync = vsync, | |
_length = length, | |
_initialIndex = initialIndex; | |
final int _initialIndex; | |
final int _length; | |
final TickerProvider _vsync; | |
@override | |
TabController createDefaultValue() { | |
return TabController( | |
initialIndex: _initialIndex, length: _length, vsync: _vsync); | |
} | |
@override | |
TabController fromPrimitives(Object? data) { | |
final selectedIndex = data as int; | |
return TabController( | |
initialIndex: selectedIndex, length: _length, vsync: _vsync); | |
} | |
@override | |
Object toPrimitives() { | |
return value.index; | |
} | |
} | |
class RestorableTabsScreen extends StatefulWidget { | |
const RestorableTabsScreen({Key? key}) : super(key: key); | |
@override | |
_RestorableTabsScreenState createState() => _RestorableTabsScreenState(); | |
} | |
class _RestorableTabsScreenState extends State<RestorableTabsScreen> | |
with RestorationMixin, SingleTickerProviderStateMixin { | |
late RestorableTabController _restorableTabController; | |
@override | |
String? get restorationId => 'tabs_screen'; | |
@override | |
void initState() { | |
super.initState(); | |
_restorableTabController = | |
RestorableTabController(initialIndex: 0, length: 3, vsync: this); | |
} | |
@override | |
void restoreState(RestorationBucket? oldBucket, bool initialRestore) { | |
registerForRestoration(_restorableTabController, 'tab_controller'); | |
} | |
@override | |
void dispose() { | |
_restorableTabController.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Restorable Tabs'), | |
bottom: TabBar( | |
controller: _restorableTabController.value, | |
tabs: const [ | |
Tab(text: 'Tab 1'), | |
Tab(text: 'Tab 2'), | |
Tab(text: 'Tab 3'), | |
], | |
), | |
), | |
body: TabBarView( | |
controller: _restorableTabController.value, | |
children: const [ | |
Center(child: Text('Content 1')), | |
Center(child: Text('Content 2')), | |
Center(child: Text('Content 3')), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment