Line data Source code
1 : import 'package:collection/collection.dart'; 2 : import 'package:meta/meta.dart'; 3 : 4 : import 'package:widgetbook_generator/code_generators/instances/base_instance.dart'; 5 : 6 : @immutable 7 : 8 : /// Defines a lambda function instance. 9 : /// 10 : /// Example: 11 : /// `(context) => buildStory(context)` 12 : class LambdaInstance extends BaseInstance { 13 : /// Creates a new instance of [LambdaInstance] 14 4 : const LambdaInstance({ 15 : required this.name, 16 : this.parameters = const <String>[], 17 : }); 18 : 19 : /// The name of the function 20 : /// 21 : /// Example for `(context) => buildStory(context)` 22 : /// [name] would be `buildStory` 23 : final String name; 24 : 25 : /// The parameters of the function 26 : /// 27 : /// Example for `(context, index) => buildStory(context, index)` 28 : /// [parameters] would be `['context', 'index']` 29 : final List<String> parameters; 30 : 31 : /// Joins the parameters by inserting a ', ' between every instance 32 : /// 33 : /// Example: 34 : /// for the parameters `['context', 'index']` 35 : /// returns 'context, index' 36 1 : String _parametersToCode() { 37 2 : return parameters.join(', '); 38 : } 39 : 40 1 : @override 41 : String toCode() { 42 1 : final parameters = _parametersToCode(); 43 2 : return '($parameters) => $name($parameters)'; 44 : } 45 : 46 2 : @override 47 : bool operator ==(Object other) { 48 : if (identical(this, other)) return true; 49 2 : final listEquals = const DeepCollectionEquality().equals; 50 : 51 2 : return other is LambdaInstance && 52 6 : other.name == name && 53 4 : listEquals(other.parameters, parameters); 54 : } 55 : 56 0 : @override 57 0 : int get hashCode => name.hashCode ^ parameters.hashCode; 58 : }