Add test for tab_controller.1.dart API example. (#148189)
This PR contributes to https://github.com/flutter/flutter/issues/130459 ### Description - Updates `examples/api/lib/material/tab_controller/tab_controller.1.dart` to properly remove the listener from the `TabController` - Adds tests for `examples/api/lib/material/tab_controller/tab_controller.1.dart`
This commit is contained in:
committed by
GitHub
parent
6067d8f219
commit
03e6cfa7b1
@@ -11,42 +11,39 @@ void main() => runApp(const TabControllerExampleApp());
|
||||
class TabControllerExampleApp extends StatelessWidget {
|
||||
const TabControllerExampleApp({super.key});
|
||||
|
||||
static const List<Tab> tabs = <Tab>[
|
||||
Tab(text: 'Zeroth'),
|
||||
Tab(text: 'First'),
|
||||
Tab(text: 'Second'),
|
||||
];
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const MaterialApp(
|
||||
home: TabControllerExample(),
|
||||
home: TabControllerExample(tabs: tabs),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const List<Tab> tabs = <Tab>[
|
||||
Tab(text: 'Zeroth'),
|
||||
Tab(text: 'First'),
|
||||
Tab(text: 'Second'),
|
||||
];
|
||||
|
||||
class TabControllerExample extends StatelessWidget {
|
||||
const TabControllerExample({super.key});
|
||||
const TabControllerExample({
|
||||
required this.tabs,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final List<Tab> tabs;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DefaultTabController(
|
||||
length: tabs.length,
|
||||
// The Builder widget is used to have a different BuildContext to access
|
||||
// closest DefaultTabController.
|
||||
child: Builder(builder: (BuildContext context) {
|
||||
final TabController tabController = DefaultTabController.of(context);
|
||||
tabController.addListener(() {
|
||||
if (!tabController.indexIsChanging) {
|
||||
// Your code goes here.
|
||||
// To get index of current tab use tabController.index
|
||||
}
|
||||
});
|
||||
return Scaffold(
|
||||
child: DefaultTabControllerListener(
|
||||
onTabChanged: (int index) {
|
||||
debugPrint('tab changed: $index');
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
bottom: const TabBar(
|
||||
tabs: tabs,
|
||||
),
|
||||
bottom: TabBar(tabs: tabs),
|
||||
),
|
||||
body: TabBarView(
|
||||
children: tabs.map((Tab tab) {
|
||||
@@ -58,8 +55,75 @@ class TabControllerExample extends StatelessWidget {
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class DefaultTabControllerListener extends StatefulWidget {
|
||||
const DefaultTabControllerListener({
|
||||
required this.onTabChanged,
|
||||
required this.child,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final ValueChanged<int> onTabChanged;
|
||||
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
State<DefaultTabControllerListener> createState() =>
|
||||
_DefaultTabControllerListenerState();
|
||||
}
|
||||
|
||||
class _DefaultTabControllerListenerState
|
||||
extends State<DefaultTabControllerListener> {
|
||||
TabController? _controller;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
|
||||
final TabController? defaultTabController =
|
||||
DefaultTabController.maybeOf(context);
|
||||
|
||||
assert(() {
|
||||
if (defaultTabController == null) {
|
||||
throw FlutterError(
|
||||
'No DefaultTabController for ${widget.runtimeType}.\n'
|
||||
'When creating a ${widget.runtimeType}, you must ensure that there '
|
||||
'is a DefaultTabController above the ${widget.runtimeType}.',
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}());
|
||||
|
||||
if (defaultTabController != _controller) {
|
||||
_controller?.removeListener(_listener);
|
||||
_controller = defaultTabController;
|
||||
_controller?.addListener(_listener);
|
||||
}
|
||||
}
|
||||
|
||||
void _listener() {
|
||||
final TabController? controller = _controller;
|
||||
|
||||
if (controller == null || controller.indexIsChanging) {
|
||||
return;
|
||||
}
|
||||
|
||||
widget.onTabChanged(controller.index);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller?.removeListener(_listener);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return widget.child;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user