Line data Source code
1 : import 'package:collection/collection.dart'; 2 : 3 : import 'package:widgetbook_generator/code_generators/instances/base_instance.dart'; 4 : 5 : /// An instance that allows to define multiple types of [BaseInstance]s as a 6 : /// List. 7 : /// 8 : /// [ListInstance] generates code of the form `[Instance1, Instance2, ...] 9 : class ListInstance<T extends BaseInstance> extends BaseInstance { 10 : /// Creates a new instance of [ListInstance] 11 : /// 12 : /// [instances] specifies the instances injected into the list. 13 6 : const ListInstance({ 14 : required this.instances, 15 : this.trailingComma = true, 16 : }); 17 : 18 : /// Specifies the instances injected into the list. 19 : final List<T> instances; 20 : 21 : /// Specifies if a trailing comma should be inserted into the code. 22 : /// This leads to better code formatting. 23 : final bool trailingComma; 24 : 25 1 : @override 26 : String toCode() { 27 1 : final codeOfValues = instances 28 1 : .map( 29 2 : (instance) => instance.toCode(), 30 : ) 31 1 : .toList(); 32 : 33 1 : final stringBuffer = StringBuffer() 34 1 : ..write('[') 35 1 : ..write( 36 1 : codeOfValues.join(', '), 37 : ); 38 : 39 3 : if (trailingComma && instances.isNotEmpty) { 40 1 : stringBuffer.write(','); 41 : } 42 : 43 1 : stringBuffer.write(']'); 44 1 : return stringBuffer.toString(); 45 : } 46 : 47 4 : @override 48 : bool operator ==(Object other) { 49 : if (identical(this, other)) return true; 50 4 : final listEquals = const DeepCollectionEquality().equals; 51 : 52 4 : return other is ListInstance<T> && 53 8 : listEquals(other.instances, instances) && 54 12 : other.trailingComma == trailingComma; 55 : } 56 : 57 0 : @override 58 0 : int get hashCode => instances.hashCode ^ trailingComma.hashCode; 59 : 60 0 : @override 61 : String toString() => 62 0 : 'ListInstance(instances: $instances, trailingComma: $trailingComma)'; 63 : }