Line data Source code
1 : /// Create beautiful and low-effort forms that output valid data. 2 : /// Based on Flutter / Material Design Widgets / JSON Schema. 3 : library flutter_json_schema_form; 4 : 5 : import 'package:flutter/material.dart'; 6 : import 'package:flutter_json_schema_form/widgets/flutter_json_schema_field.dart'; 7 : import 'package:json_schema_document/json_schema_document.dart'; 8 : 9 : import 'controller/flutter_json_schema_form_controller.dart'; 10 : 11 : /// A widget that displays a form based on a JSON Schema. 12 : class FlutterJsonSchemaForm extends StatelessWidget { 13 2 : const FlutterJsonSchemaForm.fromJsonSchema({ 14 : Key? key, 15 : required this.jsonSchema, 16 : this.isInnerField = false, 17 : required this.path, 18 : required this.controller, 19 : this.onSubmit, 20 : this.buttonText, 21 2 : }) : super(key: key); 22 : 23 : /// JSON Schema to use to generate the form. 24 : final JsonSchema jsonSchema; 25 : 26 : final bool isInnerField; 27 : 28 : final FlutterJsonSchemaFormController controller; 29 : 30 : final List<String> path; 31 : 32 : final String? buttonText; 33 : 34 : final Function? onSubmit; 35 : 36 2 : @override 37 : Widget build(BuildContext context) { 38 2 : return Column( 39 : crossAxisAlignment: CrossAxisAlignment.start, 40 : mainAxisAlignment: MainAxisAlignment.center, 41 2 : children: [ 42 6 : if (jsonSchema.title is String) 43 2 : Text( 44 4 : jsonSchema.title as String, 45 2 : style: isInnerField 46 3 : ? Theme.of(context).textTheme.headline6 47 3 : : Theme.of(context).textTheme.headline5, 48 : ), 49 6 : if (jsonSchema.description is String) 50 1 : Text( 51 2 : jsonSchema.description as String, 52 : ), 53 2 : const SizedBox(height: 16), 54 6 : if (jsonSchema.properties is Map) 55 6 : ...jsonSchema.properties.entries 56 2 : .map( 57 4 : (entry) => FlutterJsonSchemaFormField.fromJsonSchema( 58 2 : jsonSchema: entry.value, 59 8 : path: path.isEmpty ? [entry.key] : [...path, entry.key], 60 2 : controller: controller, 61 : editingControllerMapping: 62 4 : controller.textEditingControllerMapping, 63 : ), 64 : ) 65 2 : .toList(), 66 2 : isInnerField 67 3 : ? Container() 68 1 : : Row( 69 : mainAxisAlignment: MainAxisAlignment.center, 70 1 : children: [ 71 1 : ElevatedButton( 72 0 : onPressed: () { 73 0 : if (onSubmit != null) { 74 0 : onSubmit!(); 75 : } 76 : }, 77 2 : child: Text(buttonText ?? 'Submit'), 78 : ), 79 : ], 80 : ), 81 : ], 82 : ); 83 : } 84 : }