Closed vijaymsc closed 4 months ago
main.dart import 'dart:convert'; import 'package:json_dynamic_widget/json_dynamic_widget.dart'; import 'package:http/http.dart' as http; void main() { launcher(); } void launcher() { WidgetsFlutterBinding.ensureInitialized(); final registry = JsonWidgetRegistry.instance; Map<String, JsonWidgetFunction> registryFunc = { "validateForm": ({args, required registry}) => () async { final BuildContext context = registry.getValue(args![0]); bool valid = Form.of(context).validate(); if (valid) { var userName = registry.getValue('user_email'); var userPassword = registry.getValue('user_password'); bool status = await validateUserData(userName, userPassword); if(status) { registry.setValue('user_email', ''); registry.setValue('user_password', ''); } showSnackBar(context,status?"Login Success": "Login Failed"); } } }; registry.registerFunctions(registryFunc); runApp(const MaterialApp( debugShowCheckedModeBanner: false, home: AuthLogin(), )); } class AuthLogin extends StatelessWidget { const AuthLogin({super.key}); @override Widget build(BuildContext context) { return FutureBuilder<String>( future: getJsonWidget(), builder: (context, snapShot) { if (snapShot.connectionState == ConnectionState.done) { final registry = JsonWidgetRegistry.instance.copyWith(); final widget = JsonWidgetData.fromDynamic( json.decode(snapShot.data!), registry: registry, ); return widget.build(context: context); } return const Center(child: CircularProgressIndicator()); }); } Future<String> getJsonWidget() async { String jsonData = await rootBundle.loadString('assets/example/user_auth.json'); return jsonData; } } Future<bool> validateUserData( String userEmail, String password) async { var map = {"email": userEmail, "password": password}; print(map); var result = await http.post(Uri.parse('https://reqres.in/api/login'), body: map); print('${result.statusCode}'); if (result.statusCode == 200 || result.statusCode == 201) { return true; } return false; } showSnackBar(BuildContext context,String content){ ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(content))); } user_auth.json { "type": "scaffold", "args": { "backgroundColor": "#C0C0C0", "appBar": { "type": "app_bar", "args": { "backgroundColor": "#C0C0C0", "title": { "type": "text", "args": { "text": "User Login" } } } }, "body": { "type": "padding", "args": { "padding": 25, "child": { "type": "center", "args": { "child": { "type": "column", "args": { "mainAxisAlignment": "center", "children": [ { "type": "text", "args": { "text": "Welcome", "style": { "fontSize": 30, "fontWeight": "bold" } } }, { "type": "sized_box", "args": { "height": 20.0 } }, { "type": "form", "args": { "child": { "type": "column", "args": { "children": [ { "type": "text_form_field", "id": "user_email", "args": { "autovalidateMode": "onUserInteraction", "initialValue": "eve.holt@reqres.in", "decoration": { "hintText": "Enter User Email....", "labelText": "User Email", "border": { "type": "outline", "borderRadius": 15.0 } }, "validators": [ { "type": "required" }, { "type": "email" } ] } }, { "type": "sized_box", "args": { "height": 30.0 } }, { "type": "text_form_field", "id": "user_password", "args": { "obscureText": true, "initialValue": "cityslicka", "decoration": { "hintText": "Enter User password....", "labelText": "User Password", "border": { "type": "outline", "borderRadius": 15.0 } }, "validators": [ { "type": "required" } ] } }, { "type": "sized_box", "args": { "height": 30.0 } }, { "type": "elevated_button", "id": "submit_button", "listen": ["user_email","user_password"], "args": { "onPressed": "${validateForm('form_context','https://reqres.in/api/login',{'email':'user_email','password':'user_password'})}", "style": { "backgroundColor": "#0000FF" }, "child": { "type": "save_context", "args": { "key": "form_context", "child": { "type": "text", "args": { "text": "Login", "style": { "fontSize": 25, "color": "#FFFFFF", "fontWeight": "bold" } } } } } } } ] } } } } ] } } } } } } } }
Output
{email: null, password: null}