-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugins): add logging plugin that allows writing logs to storybo…
…ok (#131)
- Loading branch information
Showing
6 changed files
with
104 additions
and
0 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
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,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; | ||
} |
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
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
Binary file modified
BIN
+207 Bytes
(100%)
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.