Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugins): add logging plugin that allows writing logs to storybook #131

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading