Changing Widgets (CallBack)

 //import "package:flutter/material.dart";


// void main() {
// runApp(
// MaterialApp(
// home: Scaffold(
// body: Center(
// child: Counter(),
// ),
// ),
// ),
// );
// }
// class Counter extends StatefulWidget {
// const Counter({super.key});
//
// State<Counter> createState() {
// return _CounterState();
// }
// }
// class _CounterState extends State<Counter> {
// int _counter = 0;
// void _increment() {
// setState((){
// _counter ++;
// });
// }
// @override
// Widget build(BuildContext context) {
// return Row(
// mainAxisAlignment: MainAxisAlignment.center,
// children: [
// ElevatedButton(
// onPressed: (){
// _increment();
// },
// child: Text("Increment"),
// ),
// SizedBox(width: 40.0,),
// Text("Count: $_counter"),
// ],
// );
// }
// }

/// In Flutter, communication between widgets can use VoidCallback and Function Callback.
//
/// VoidCallbacks are callbacks that don't return a value to the Parent Widget.
// This is useful if we only want to notify the Parent Widget of events that occurred on the Child Widget.
//
// The Function Callback is a callback that notifies the Parent Widget of an event
// that occurs in the Child Widget and also returns a value to the Parent Widget.

/// typedef VoidCallback = void Function();
/// VoidCallbacks and Function Callbacks are both functions.
/// The only difference is that VoidCallback has no arguments and returns no data.
// VoidCallback is a function which takes no parameters and returns no parameters.
// In Flutter it is also true. Sometimes we call it simply callback.

/// Just like any other data type in Dart, we can use the Function data type to assign a value to a variable.
/// Even we can pass that Function variable through Class Constructor.

/// 1. CREATE A NEW FLUTTER PROJECT
// void main() {
// runApp(const MyApp());
// }
// class MyApp extends StatelessWidget {
// const MyApp({super.key});
//
// @override
// Widget build(BuildContext context) {
// return MaterialApp(
// title: "Flutter Demo",
// debugShowCheckedModeBanner: false,
// theme: ThemeData(
// primarySwatch: Colors.blue,
// ),
// home: const ParentWidgetPage(title: "Flutter VoidCallback & Function Callback"),
// );
// }
// }

/// 2. CREATE A PARENT WIDGET
// class ParentWidgetPage extends StatefulWidget {
// const ParentWidgetPage({super.key, required this.title});
// final String title;
//
// @override
// State<ParentWidgetPage> createState() {
// return ParentWidgetPageState();
// }
// }
// class ParentWidgetPageState extends State<ParentWidgetPage> {
// int _counter = 0;
//
// void _voidCallback() {
// setState((){
// _counter++;
// });
// }
//
// void _functionCallback(int i) {
// setState((){
// _counter += i;
// });
// }
//
// @override
// Widget build(BuildContext context) {
// return Scaffold(
// appBar: AppBar(
// title: Text(widget.title),
// ),
// 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,),
// ],
// ),
// ),
// floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
// floatingActionButton: Padding(
// padding: const EdgeInsets.all(8.0,),
// child: Row(
// mainAxisAlignment: MainAxisAlignment.spaceBetween,
// children: <Widget>[
// VoidChildWidgetPage(
// voidCallback: _voidCallback,
// ),
// FunctionChildWidgetPage(
// functionCallback: _functionCallback,
// ),
// ],
// ),
// ),
// );
// }
// }

/// 3. CREATE A VoidCallback CHILD WIDGET
// class VoidChildWidgetPage extends StatelessWidget {
// final VoidCallback voidCallback;
//
// const VoidChildWidgetPage({super.key, required this.voidCallback});
//
// @override
// Widget build(BuildContext context) {
// return ElevatedButton(
// onPressed: () {
// return voidCallback();
// },
// child: const Text("Void Callback"),
// );
// }
// }

/// 4. CREATE A FUNCTION CALLBACK CHILD WIDGET
// class FunctionChildWidgetPage extends StatelessWidget {
// final Function(int) functionCallback;
//
// const FunctionChildWidgetPage({super.key, required this.functionCallback});
//
// @override
// Widget build(BuildContext context) {
// return ElevatedButton(
// onPressed: () {
// return functionCallback(5);
// },
// child: const Text("Fx Callback"),
// );
// }
// }

/// ANOTHER EXAMPLE OF COUNTER APPLICATION USING VoidCallback
// void main() {
// runApp(
// const MaterialApp(
// debugShowCheckedModeBanner: false,
// home: Scaffold(
// body: Counter(),
// ),
// ),
// );
// }
// class CounterDisplay extends StatelessWidget {
// const CounterDisplay({super.key, required this.count});
//
// final int count;
//
// @override
// Widget build(BuildContext context) {
// return Text("Count: $count",
// style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 30,),);
// }
// }
//
// class CounterIncrementor extends StatelessWidget {
// const CounterIncrementor({super.key, required this.onPres});
//
// final VoidCallback onPres;
//
// @override
// Widget build(BuildContext context) {
// return ElevatedButton(
// onPressed: () {
// return onPres();
// },
// child: const Text("Increment",
// style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30,),),
// );
// }
// }
// class Counter extends StatefulWidget {
// const Counter({super.key,});
//
// @override
// State<Counter> createState() {
// return _CounterState();
// }
// }
// class _CounterState extends State<Counter> {
// int _counter = 0;
//
// void _increment() {
// setState((){
// _counter++;
// });
// }
//
// @override
// Widget build(BuildContext context) {
// return Center(
// child: Row(
// mainAxisAlignment: MainAxisAlignment.center,
// children: <Widget>[
// CounterIncrementor(
// onPres: _increment,
// ),
// const SizedBox(width: 50,),
// CounterDisplay(
// count: _counter,
// ),
// ],
// ),
// );
// }
// }

/// Example of "Function as First Class Object"
void main() {
int resultOfAddition = calculate(10, 20, add);
print(resultOfAddition);

int resultOfSubtraction = calculate(10, 20, sub);
print(resultOfSubtraction);
}

int add(int num1, int num2) {
int result = num1 + num2;
return result;
}

int sub(int num1, int num2) {
int result = num1 - num2;
return result;
}

int calculate(int x, int y, Function performCalculation) {
return performCalculation(x, y);
}

/// In the above code, we have used function as a first class citizen, or first class object.
/// In other words, we have passed a function just like any variable.
//
/// In Dart, function is treated like any other variable. As a result,
/// we can call the Dart programming language has First-class functions.
//
/// It’s a different concept, that we need to understand in depth.
//
// However, at present, let us just remember that, we can pass a function as a parameter.
// Therefore, we can pass it as a class constructor as well.

/// try to understand the above code in the light of the code below.
// void main() {
// calculate(20, 20, add,);
// calculate(20, 40, sub,);
// }
//
// void add(int num1, int num2) {
//
// int result = num1 + num2;
// print(result);
// }
//
// void sub(int num1, int num2) {
// int result = num1 - num2;
// print(result);
// }
//
// void calculate(int x, int y, performCalculation) {
//
// performCalculation(x, y); //add and sub method will call from here.x and y value will pass into the num1 and num2
// }

/// In the first code sample, we have used functions that returns value.
/// Rather, to be specific, they return a certain data type – integer.
/// But when we rewrite the code, we pass a function just as a variable. So it makes more sense.
//
// Therefore we can say this.
/// VoidCallback = void Function()

// That means, it is a signature of callbacks that have no arguments and return no data.
// As regards to our above code it is closer to void callback. But not exactly the void callback.
//
// Because our callbacks have arguments, although they return no data in particular.

/// What is callback function and how it works?
// Consequently, we can say that a callback function is a function passed into another function as an argument.
//
// After that, when it is invoked inside the outer function, it completes some kind of routine or action.
//
// In Flutter exactly that happens.

/// What are callbacks in flutter?
// Now, we have learned that callback is basically a function or a method that
// we pass as a variable into another function, or method to perform an action.
//
// Simply put, we send data using the callback or VoidCallback.

/// What is Void Callback flutter?
// In Flutter the case is slightly different. Because we are handling Widgets, or classes.
//
// Therefore, the VoidCallback is useful for callback events with no expected value. But it will perform some tasks.

Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

Pagination First Practical in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter