5

child passing data to parent and parent send something back to child. (ver 1)

 2 years ago
source link: https://gist.github.com/SyLiz/48ccb33630ef5568c85246330bb1787b
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
child passing data to parent and parent send something back to child. (ver 1) · GitHub
child passing data to parent and parent send something back to child. (ver 1)

import 'package:flutter/material.dart';

void main() { runApp(MyApp()); }

class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key);

@override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: ParentWidget(), ); } }

class ParentWidget extends StatelessWidget { const ParentWidget({Key? key}) : super(key: key); final _secretValue = 'This is Top Secret Data';

void _handleRequestFromChild( {required Function onFailed, required Function(String) onSuccess, required String valueFromChild}) { if (valueFromChild == '1234') { onSuccess(_secretValue); } else { onFailed(); } }

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('ParentWidget'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ TextButton( onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => ChildWidget(onGetSecretData: (valueFromChild) { if (valueFromChild == '1234') return _secretValue; else return '404'; }))); }, child: Text('Go to child'), ), ], ), ), ); } }

class ChildWidget extends StatelessWidget { final Function(String) onGetSecretData; const ChildWidget({Key? key, required this.onGetSecretData}) : super(key: key);

@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('ChildWidget'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ TextButton( onPressed: () { String valueFromParent = onGetSecretData('1234'); print(valueFromParent); }, child: Text('Get secret Data from Parent')) ], ), ), ); } }


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK