Skip to content

Commit

Permalink
feat(plugins): add logging plugin that allows writing logs to storybo…
Browse files Browse the repository at this point in the history
…ok (#131)
  • Loading branch information
buehler committed Mar 11, 2024
1 parent 14cd9dd commit a77b2e4
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/storybook_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ class MyApp extends StatelessWidget {
name: 'Story/Nested/Multiple/Fourth',
builder: (context) => const Center(child: Text('Fourth')),
),
Story(
name: 'Story/Logging',
builder: (context) => Center(
child: ElevatedButton(
onPressed: () => context.logger.log('This is a test message'),
child: const Text('Log something'),
),
),
),
Story(
name: 'Story without a category',
builder: (context) => const Center(child: Text('Simple text')),
Expand Down
92 changes: 92 additions & 0 deletions packages/storybook_flutter/lib/src/plugins/logging.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:storybook_flutter/src/plugins/plugin.dart';

/// Plugin that allows logging from stories and callbacks.
/// Shows the recorded logs inside a panel.
///
/// With this plugin, one can also test actions inside the story and
/// then see the logs appear in the panel.
class LoggingPlugin extends Plugin {
const LoggingPlugin()
: super(
icon: _buildIcon,
wrapperBuilder: _buildWrapper,
onPressed: _onPressed,
);
}

abstract interface class Logger {
void log(String message);
}

class LoggingNotifier extends ChangeNotifier implements Logger {
final _logs = <String>[];
var _panelVisible = false;

List<String> get logs => List.unmodifiable(_logs);

bool get panelVisible => _panelVisible;

set panelVisible(bool value) {
_panelVisible = value;
notifyListeners();
}

@override
void log(String message) {
_logs.insert(0, '${DateTime.now().toIso8601String()} - $message');
notifyListeners();
}
}

extension Logging on BuildContext {
Logger get logger => read<LoggingNotifier>();
}

Widget? _buildIcon(BuildContext context) => const Icon(
Icons.format_quote,
semanticLabel: 'Log output',
);

Widget _buildWrapper(BuildContext context, Widget? child) =>
ChangeNotifierProvider(
create: (context) => LoggingNotifier(),
child: Column(
children: [
Expanded(flex: 3, child: child ?? const SizedBox.shrink()),
Consumer<LoggingNotifier>(
builder: (context, notifier, child) => notifier.panelVisible
? Flexible(
flex: 1,
child: Container(
color: Colors.white,
padding: const EdgeInsets.symmetric(
vertical: 8,
horizontal: 16,
),
child: ListView.builder(
itemCount: notifier.logs.length,
itemBuilder: (context, index) => Text(
notifier.logs[index],
style: const TextStyle(
fontSize: 14,
color: Colors.black,
height: 1.4,
fontFamily: 'Courier',
fontFamilyFallback: ['monospace'],
),
),
),
),
)
: const SizedBox.shrink(),
),
],
),
);

void _onPressed(BuildContext context) {
final notifier = context.read<LoggingNotifier>();
notifier.panelVisible = !notifier.panelVisible;
}
2 changes: 2 additions & 0 deletions packages/storybook_flutter/lib/src/storybook.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:nested/nested.dart';
import 'package:provider/provider.dart';
import 'package:storybook_flutter/src/plugins/logging.dart';
import 'package:storybook_flutter/src/plugins/plugin.dart';
import 'package:storybook_flutter/src/plugins/plugin_panel.dart';
import 'package:storybook_flutter/src/story.dart';
Expand Down Expand Up @@ -36,6 +37,7 @@ class Storybook extends StatefulWidget {
LayoutPlugin(initialLayout),
const ContentsPlugin(),
const KnobsPlugin(),
const LoggingPlugin(),
...plugins ?? _defaultPlugins,
]),
stories = UnmodifiableListView(stories);
Expand Down
1 change: 1 addition & 0 deletions packages/storybook_flutter/lib/storybook_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export 'src/knobs/knobs.dart';
export 'src/knobs/select_knob.dart';
export 'src/knobs/slider_knob.dart';
export 'src/knobs/string_knob.dart';
export 'src/plugins/logging.dart';
export 'src/plugins/plugin.dart';
export 'src/plugins/plugin_panel.dart';
export 'src/story.dart';
Expand Down
Binary file modified packages/storybook_flutter/test/goldens/simple_story_layout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/storybook_flutter/test/goldens/story_layout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a77b2e4

Please sign in to comment.