ookami-kb / storybook_flutter

A storybook for Flutter widgets.
https://pub.dev/packages/storybook_flutter
MIT License
288 stars 66 forks source link

Not working context.knobs.sliderInt #72

Closed afl-dev closed 2 years ago

afl-dev commented 2 years ago

storybook_flutter: ^0.10.0+1 sample


    return Story(
      name: 'Screens/Scaffold0',
      description: 'Story with scaffold and different knobs..',
      builder: (context) {
        return Scaffold(
            body: SizedBox(
          width: double.infinity,
          child: context.knobs.options<Widget>(
            label: 'Parent',
            initial: Column(
                children: List.generate(
              context.knobs.sliderInt(
                label: 'Items count Column',
                initial: 2,
                min: 1,
                max: 10,
                description: 'Number of items',
              ),
              (_) => child,
            )),
            options: [
              Option(
                label: 'Column',
                value: Column(
                    children: List.generate(
                  context.knobs.sliderInt(
                    label: 'Items count Column',
                    initial: 2,
                    min: 1,
                    max: 10,
                    description: 'Number of items',
                  ),
                  (_) => child,
                )),
              ),
              Option(
                label: 'Row',
                value: Row(
                  children: List.generate(
                    context.knobs.sliderInt(
                      label: 'Items count Row',
                      initial: 1,
                      min: 1,
                      max: 10,
                      description: 'Number of items',
                    ),
                    (_) => child,
                  ),
                ),
              ),
              Option(
                label: 'ListView',
                value: ListView(
                  children: List.generate(
                    context.knobs.sliderInt(
                      label: 'Items count ListView',
                      initial: 1,
                      min: 1,
                      max: 10,
                      description: 'Number of items',
                    ),
                    (_) => child,
                  ),
                ),
              ),
            ],
          ),
        ));
      },
    );
  }```
ookami-kb commented 2 years ago

Hey, do you get some error there?

Cause it's working on the demo app here: https://ookami-kb.github.io/storybook_flutter/#/

afl-dev commented 2 years ago

Storybook(
      plugins: initializePlugins(
        contentsSidePanel: true,
        knobsSidePanel: true,
      ),
      initialStory: 'Simple/ActionButton',
      stories: [
    _parents(context, _iconButton(context)), 
      ],
    );
  }
Widget _iconButton(BuildContext context) {
    return simple.IconButton(
      iconSize: context.knobs.slider(label: 'iconSize', max: 256, initial: 24),
      splashRadius:
          context.knobs.slider(label: 'splashRadius', max: 256, initial: 24),
      icon: context.knobs.options(
        label: 'Icons',
        initial: Icons.arrow_back,
        options: const [
          Option(
            label: 'Icons.arrow_back',
            value: Icons.arrow_back,
            description: 'icon',
          ),
          Option(
            label: 'Icons.portrait',
            value: Icons.portrait,
            description: 'icon',
          ),
        ],
      ),
      iconButtonStyle: simple.IconButtonStyle(
        color: context.knobs.options(
          label: 'IconButton color',
          initial: Colors.blue,
          options: const [
            Option(
              label: 'Colors.blue',
              value: Colors.blue,
              description: 'Blue color',
            ),
            Option(
              label: 'Colors.green',
              value: Colors.green,
              description: 'Green color',
            ),
          ],
        ),
        disabledColor: context.knobs.options(
          label: 'disabledColor',
          initial: Colors.black12,
          options: const [
            Option(
              label: 'Colors.black12',
              value: Colors.black12,
            ),
            Option(
              label: 'Colors.grey',
              value: Colors.grey,
            ),
          ],
        ),
      ),
      onPressed:
          context.knobs.boolean(label: 'enable', initial: true) ? () {} : null,
      actionInProgress: context.knobs.boolean(label: 'actionInProgress'),
    );
  }
afl-dev commented 2 years ago

======== Exception caught by widgets library ======================================================= The following ProviderNotFoundException was thrown building MyApp(dirty): Error: Could not find the correct Provider above this MyApp Widget

This happens because you used a BuildContext that does not include the provider of your choice. There are a few common scenarios:

If none of these solutions work, consider asking for help on StackOverflow: https://stackoverflow.com/questions/tagged/flutter

The relevant error-causing widget was:

When the exception was thrown, this was the stack:

0 Provider._inheritedElementOf (package:provider/src/provider.dart:356:7)

1 Provider.of (package:provider/src/provider.dart:293:30)

2 WatchContext.watch (package:provider/src/provider.dart:700:21)

3 Knobs.knobs (package:storybook_flutter/src/plugins/knobs.dart:95:29)

4 MyApp._iconButton (package:components_book/main.dart:225:25)

5 MyApp.build (package:components_book/main.dart:57:27)

6 StatelessElement.build (package:flutter/src/widgets/framework.dart:4662:28)

7 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4588:15)

8 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)

9 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4566:5)

10 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4561:5)

11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3631:14)

12 Element.updateChild (package:flutter/src/widgets/framework.dart:3383:18)

13 RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1198:16)

14 RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1167:5)

15 RenderObjectToWidgetAdapter.attachToRenderTree. (package:flutter/src/widgets/binding.dart:1112:18)

16 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2531:19)

17 RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1111:13)

18 WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:944:7)

19 WidgetsBinding.scheduleAttachRootWidget. (package:flutter/src/widgets/binding.dart:924:7)

(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)

======== Exception caught by widgets library ======================================================= The following ProviderNotFoundException was thrown building MyApp(dirty): Error: Could not find the correct Provider above this MyApp Widget

This happens because you used a BuildContext that does not include the provider of your choice. There are a few common scenarios:

If none of these solutions work, consider asking for help on StackOverflow: https://stackoverflow.com/questions/tagged/flutter

The relevant error-causing widget was:

When the exception was thrown, this was the stack:

0 Provider._inheritedElementOf (package:provider/src/provider.dart:356:7)

1 Provider.of (package:provider/src/provider.dart:293:30)

2 WatchContext.watch (package:provider/src/provider.dart:700:21)

3 Knobs.knobs (package:storybook_flutter/src/plugins/knobs.dart:95:29)

4 MyApp._iconButton (package:components_book/main.dart:225:25)

5 MyApp.build (package:components_book/main.dart:57:27)

6 StatelessElement.build (package:flutter/src/widgets/framework.dart:4662:28)

7 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4588:15)

8 Element.rebuild (package:flutter/src/widgets/framework.dart:4311:5)

9 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2578:33)

10 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:882:21)

11 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)

12 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1145:15)

13 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1082:9)

14 SchedulerBinding.scheduleWarmUpFrame. (package:flutter/src/scheduler/binding.dart:863:7)

(elided 4 frames from class _RawReceivePortImpl, class _Timer, and dart:async-patch)

afl-dev commented 2 years ago

Can you please share how you create the Storybook widget?

please see above

ookami-kb commented 2 years ago

Yeah, I see it now.

_parents(context, _iconButton(context)), 

It won't work like this. You shouldn't pass the context to the function creating the Widget. When you're using Story() constructor, you've got a builder parameter that passes the right context – you should use it to get access to knobs.

ookami-kb commented 2 years ago

Closing the issue, working as expected.