Last active
May 19, 2022 14:30
-
-
Save DaisukeNagata/3a6a4132b476df9bf10e22ee2494877b to your computer and use it in GitHub Desktop.
check call back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({Key? key, required this.title}) : super(key: key); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
int _counter = 0; | |
Color _w = Colors.white; | |
VoidCallback? callback; | |
Function(int)? callback2; | |
Function(List<dynamic>)? callback3; | |
CallBackLogic _callBackLogic = CallBackLogic(); | |
_buttonTap() { | |
callback3(e) { | |
setState(() { | |
_w = Colors.cyan; | |
List<dynamic> d = e as List<dynamic>; | |
_counter = int.parse(d.first); | |
_counter += double.parse(d.last).round(); | |
_showMyDialog(); | |
}); | |
} | |
callback2(e) { | |
setState(() { | |
Navigator.of(context).pop(); | |
_w = Colors.yellow; | |
_counter = e; | |
_callBackLogic.clallBackLogic3(callback3); | |
}); | |
} | |
callback() { | |
setState(() { | |
_counter = 1; | |
_showMyDialog(); | |
_w = Colors.red; | |
_callBackLogic.clallBackLogic2(callback2); | |
}); | |
} | |
_callBackLogic.clallBackLogic(callback); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
backgroundColor: _w, | |
), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
const Text( | |
'You have pushed the button this many times:', | |
), | |
Text( | |
'$_counter', | |
style: Theme.of(context).textTheme.headline4, | |
), | |
TextButton.icon( | |
onPressed: () => _buttonTap(), | |
icon: Icon(Icons.tab), | |
label: Text('Call Back Check'), | |
), | |
], | |
), | |
), | |
); | |
} | |
Future<void> _showMyDialog() async { | |
return showDialog<void>( | |
context: context, | |
barrierDismissible: false, // user must tap button! | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: Text(_counter.toString()), | |
content: SingleChildScrollView( | |
child: ListBody( | |
children: const <Widget>[ | |
Text('This is a demo alert dialog.'), | |
Text('Would you like to approve of this message?'), | |
], | |
), | |
), | |
actions: <Widget>[ | |
TextButton( | |
child: const Text('Approve'), | |
onPressed: () { | |
Navigator.of(context).pop(); | |
_buttonTap(); | |
}, | |
), | |
], | |
); | |
}, | |
); | |
} | |
} | |
class CallBackLogic { | |
clallBackLogic(VoidCallback? callback) { | |
Future.delayed(Duration(milliseconds: 1000)).then((_) => callback?.call()); | |
} | |
clallBackLogic2(Function(int)? callback) { | |
Future.delayed(Duration(milliseconds: 1000)).then((_) => callback?.call(2)); | |
} | |
clallBackLogic3(Function(List<dynamic>)? callback) { | |
Future.delayed(Duration(milliseconds: 1000)) | |
.then((_) => callback?.call(["1", "1.9"])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2022-05-19.23.17.47.mov