-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
129 additions
and
17 deletions.
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
packages/flutter_hooks/lib/src/expansion_tile_controller.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
part of 'hooks.dart'; | ||
|
||
/// Creates a [ExpansionTileController] that will be disposed automatically. | ||
/// | ||
/// See also: | ||
/// - [ExpansionTileController] | ||
ExpansionTileController useExpansionTileController({List<Object?>? keys}) { | ||
return use(_ExpansionTileControllerHook(keys: keys)); | ||
} | ||
|
||
class _ExpansionTileControllerHook extends Hook<ExpansionTileController> { | ||
const _ExpansionTileControllerHook({List<Object?>? keys}) : super(keys: keys); | ||
|
||
@override | ||
HookState<ExpansionTileController, Hook<ExpansionTileController>> | ||
createState() => _ExpansionTileControllerHookState(); | ||
} | ||
|
||
class _ExpansionTileControllerHookState | ||
extends HookState<ExpansionTileController, _ExpansionTileControllerHook> { | ||
final controller = ExpansionTileController(); | ||
|
||
@override | ||
String get debugLabel => 'useExpansionTileController'; | ||
|
||
@override | ||
ExpansionTileController build(BuildContext context) => controller; | ||
} |
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
82 changes: 82 additions & 0 deletions
82
packages/flutter_hooks/test/use_expansion_tile_controller_test.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_hooks/src/framework.dart'; | ||
import 'package:flutter_hooks/src/hooks.dart'; | ||
|
||
import 'mock.dart'; | ||
|
||
void main() { | ||
testWidgets('debugFillProperties', (tester) async { | ||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
useExpansionTileController(); | ||
return const SizedBox(); | ||
}), | ||
); | ||
|
||
await tester.pump(); | ||
|
||
final element = tester.element(find.byType(HookBuilder)); | ||
|
||
expect( | ||
element | ||
.toDiagnosticsNode(style: DiagnosticsTreeStyle.offstage) | ||
.toStringDeep(), | ||
equalsIgnoringHashCodes( | ||
'HookBuilder\n' | ||
" │ useExpansionTileController: Instance of 'ExpansionTileController'\n" | ||
' └SizedBox(renderObject: RenderConstrainedBox#00000)\n', | ||
), | ||
); | ||
}); | ||
|
||
group('useExpansionTileController', () { | ||
testWidgets('initial values matches with real constructor', (tester) async { | ||
late ExpansionTileController controller; | ||
final controller2 = ExpansionTileController(); | ||
|
||
await tester.pumpWidget(MaterialApp( | ||
home: Scaffold( | ||
body: HookBuilder(builder: (context) { | ||
controller = useExpansionTileController(); | ||
return Column( | ||
children: [ | ||
ExpansionTile( | ||
controller: controller, | ||
title: const Text('Expansion Tile'), | ||
), | ||
ExpansionTile( | ||
controller: controller2, | ||
title: const Text('Expansion Tile 2'), | ||
), | ||
], | ||
); | ||
}), | ||
), | ||
)); | ||
expect(controller, isA<ExpansionTileController>()); | ||
expect(controller.isExpanded, controller2.isExpanded); | ||
}); | ||
|
||
testWidgets('check expansion/collapse of tile', (tester) async { | ||
late ExpansionTileController controller; | ||
await tester.pumpWidget(MaterialApp( | ||
home: Scaffold( | ||
body: HookBuilder(builder: (context) { | ||
controller = useExpansionTileController(); | ||
return ExpansionTile( | ||
controller: controller, | ||
title: const Text('Expansion Tile'), | ||
); | ||
}), | ||
), | ||
)); | ||
|
||
expect(controller.isExpanded, false); | ||
controller.expand(); | ||
expect(controller.isExpanded, true); | ||
controller.collapse(); | ||
expect(controller.isExpanded, false); | ||
}); | ||
}); | ||
} |