From 0d56b6b9a5538093254d27ec1878c7da4d97aca0 Mon Sep 17 00:00:00 2001 From: robiness Date: Wed, 19 Jul 2023 11:45:09 +0200 Subject: [PATCH] Introduce passing a initial stage data without a controller --- example/test/widget_test.dart | 12 +++++------- lib/src/widgets/stage_craft.dart | 33 +++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/example/test/widget_test.dart b/example/test/widget_test.dart index 124a42d..2a1dd90 100644 --- a/example/test/widget_test.dart +++ b/example/test/widget_test.dart @@ -12,26 +12,24 @@ import 'package:stage_craft/stage_craft.dart'; void main() { testWidgets('Counter increments smoke test', (WidgetTester tester) async { - final StageController controller = StageController(); - await tester.pumpWidgetData(controller); - controller.selectWidget(MyOtherWidgetStageData()); - await tester.pumpAndSettle(); - expect(find.text('MyOtherWidget'), findsOneWidget); + await tester.pumpWidgetData(MyOtherWidgetStageData()); + expect(find.text('MyOtherWidget'), findsNWidgets(2)); }); } extension WidgetTesterExtension on WidgetTester { Future pumpWidgetData( - StageController controller, + WidgetStageData stageData, ) async { await pumpWidget( MaterialApp( home: Scaffold( body: StageCraft( - stageController: controller, + stageData: stageData, ), ), ), ); + await pumpAndSettle(); } } diff --git a/lib/src/widgets/stage_craft.dart b/lib/src/widgets/stage_craft.dart index e9590e9..951c373 100644 --- a/lib/src/widgets/stage_craft.dart +++ b/lib/src/widgets/stage_craft.dart @@ -8,13 +8,14 @@ import 'package:stage_craft/src/widgets/stage_area.dart'; /// /// Use this to create a stage for your widgets. /// -class StageCraft extends StatelessWidget { +class StageCraft extends StatefulWidget { StageCraft({ super.key, - required this.stageController, + this.stageController, this.configurationBarFooter, Size? stageSize, StageCraftSettings? settings, + this.stageData, }) : stageSize = stageSize ?? const Size(600, 800), settings = settings ?? StageCraftSettings( @@ -25,7 +26,7 @@ class StageCraft extends StatelessWidget { /// The [StageController] that controls the stage. /// /// Create one above the [StageCraft] widget and pass it here to react to stage events or set stage properties. - final StageController stageController; + final StageController? stageController; /// The size of the stage. final Size stageSize; @@ -36,19 +37,37 @@ class StageCraft extends StatelessWidget { /// An optional footer of the configuration bar. final Widget? configurationBarFooter; + /// The initially selected stage data. + final WidgetStageData? stageData; + + @override + State createState() => _StageCraftState(); +} + +class _StageCraftState extends State { + late final _stageController = widget.stageController ?? StageController(); + + @override + void initState() { + super.initState(); + if (widget.stageData != null) { + _stageController.selectWidget(widget.stageData!); + } + } + @override Widget build(BuildContext context) { return Row( children: [ StageArea( - stageController: stageController, - settings: settings, + stageController: _stageController, + settings: widget.settings, ), Align( alignment: Alignment.topCenter, child: ConfigurationBar( - controller: stageController, - configurationBarFooter: configurationBarFooter, + controller: _stageController, + configurationBarFooter: widget.configurationBarFooter, ), ), ],