So i have a Screen, HomeScreen
. Now i have another class named LoginUtil
which performs some operation. I am calling its method 'login' from Homescreen
which works totally fine. But whatever response i get in LoginUtil
... i want to access it in HomeScreen
but i am not sure as to how to do it.
HomeScreen
class HomeScreen extends StatefulWidget {
@override
State < HomeScreen > createState() => HomeScreenState();
}
class HomeScreenState extends State < HomeScreen > {
var advertisingMessage = "Click here";
static
const advertisingUUID = "0000A002-0000-1000-8000-00805f9b34fb";
static
const bleChannel = MethodChannel('ble');
final LoginUtil loginUtil = LoginUtil();
var connection = 0;
var connectionImage =
"https://assets3.lottiefiles.com/packages/lf20_lbmtnnap.json";
@override
Widget build(BuildContext context) {
performBleOperations();
return Scaffold(
drawer: NavigationDrawerWidget(),
appBar: AppBar(
title: Text("Home"),
actions: [
IconButton(
icon: Icon(Icons.notifications),
onPressed: () {},
),
InkWell(
onTap: () {
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => ScanScreen()));
},
child: Container(
margin: EdgeInsets.only(right: 10),
child: Image.asset("assets/scanner.png", height: 20, width: 20),
),
)
],
),
body: Column(
children: [
const SizedBox(height: 50.0),
Text("Connected Devices: 0",
textAlign: TextAlign.center, style: TextStyle(fontSize: 19)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
InkWell(
onTap: () {
showAdvertisingBottomSheet();
},
child: Stack(
alignment: Alignment.center,
children: [
Lottie.network(connectionImage,
height: 180.0, width: 180.0),
Text(advertisingMessage)
],
),
),
],
),
Text("Waiting for a device to connect"),
Container(
width: double.infinity,
color: const Color(0xFF09419B),
child: Text("See Logs",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white)),
margin: EdgeInsets.only(top: 20, left: 50, right: 50),
padding: EdgeInsets.all(20),
),
Container(
width: double.infinity,
color: const Color(0xFF09419B),
child: Text("Create Downlink",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white)),
margin: EdgeInsets.only(top: 20, left: 50, right: 50),
padding: EdgeInsets.all(20),
),
Container(
width: double.infinity,
color: const Color(0xFF09419B),
child: Text("Track Trips",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white)),
margin: EdgeInsets.only(top: 20, left: 50, right: 50),
padding: EdgeInsets.all(20),
),
Container(
margin: EdgeInsets.only(
top: 20,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(
children: [
Card(
child: Container(
width: 70,
height: 70,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text("12"), Text("Trucks")],
),
),
),
Text("Out For Delivery"),
],
),
Column(
children: [
Card(
child: Container(
width: 70,
height: 70,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text("9"), Text("Trucks")],
),
),
),
Text("Idle"),
],
),
Column(
children: [
Card(
child: Container(
width: 70,
height: 70,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text("122"), Text("")],
),
),
),
Text("Alerts"),
],
),
]),
),
Container(
margin: EdgeInsets.only(
top: 20,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(
children: [
Card(
child: Container(
width: 70,
height: 70,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text("1"), Text("Ongoing")],
),
),
),
Text("Incoming Trip"),
],
),
Column(
children: [
Card(
child: Container(
width: 70,
height: 70,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text("12"), Text("Ongoing")],
),
),
),
Text("Outgoing Trips"),
],
),
Column(
children: [
Card(
child: Container(
width: 70,
height: 70,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [Text("12"), Text("Trucks")],
),
),
),
Text("Out For Delivery"),
],
),
]),
),
],
),
);
}
Future < void > performBleOperations() async {
loginUtil.login("Jack", 18, callback: (String name, int age) {
// showLoading = false;
// userInfo = "name: $name, age: $age";
setState(() {});
});
}
void updateConnectionState() {
print("Trying to update the connection state");
var connectionState = loginUtil.connectionState;
if (connectionState == 0) {
setState(() {
connectionImage =
"https://assets3.lottiefiles.com/packages/lf20_cpjxufjf.json";
});
} else {
setState(() {
connectionImage =
"https://assets3.lottiefiles.com/packages/lf20_lbmtnnap.json";
});
}
}
}
LoginUtil
class LoginUtil {
LoginUtil() {
_channel.setMethodCallHandler(_methodCallHandler);
}
final MethodChannel _channel =
const MethodChannel('my-channel');
var connectionState;
Function(String, int) ? _loginSuccessCallback;
Future < void > login(String name, int age, {
Function(String, int) ? callback
}) async {
_loginSuccessCallback = callback;
var params = {
'name': name,
'age': age
};
// call native method 'login'
try {
await _channel.invokeMethod('login', params);
} catch (e) {
print(e.toString());
}
}
Future < dynamic > _methodCallHandler(MethodCall call) async {
print("Reached here my brother");
String method = call.method;
if (method == 'loginSuccess') {
if (_loginSuccessCallback != null) {
final map = call.arguments;
connectionState = map['connectionState'];
print("Connection state is: $connectionState");
}
}
}
getCurrentState() {
print("Returning current state");
return connectionState;
}
}
Any help would be highly appreciated.
CodePudding user response:
This class will give you clear answer for your question, Here you can access your parent class from your child class
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'main1.dart';
void main() {
runApp(MaterialApp(
home: Modalbtn(),
));
}
class Modalbtn extends StatefulWidget {
@override
_ModalbtnState createState() => _ModalbtnState();
}
class _ModalbtnState extends State<Modalbtn> {
String value = "0";
// Pass this method to the child page.
void _update(String newValue) {
setState(() => value = newValue);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
IconButton(
onPressed: () {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
child: Column(
children: [StatefulModalbtn(update: _update)],
),
);
});
},
icon: Icon(Icons.add),
iconSize: 20,
),
Text(
value,
style: TextStyle(fontSize: 40),
),
],
),
),
);
}
}
import 'package:flutter/material.dart';
class StatefulModalbtn extends StatelessWidget {
final ValueChanged<String> update;
StatefulModalbtn({required this.update});
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () => update("100"), // Passing value to the parent widget.
child: Text('Update (in child)'),
);
}
}