I'm using flutter for my first mobile application. I want to trigger an action without clicking a button. this is the code :
IconButton(
icon: Icon(
characteristic.isNotifying
? Icons.sync_disabled
: Icons.sync,
color: Theme.of(context).iconTheme.color?.withOpacity(0.5)),
onPressed: onNotificationPressed,
)
Thanks in advance for your help
CodePudding user response:
you can do something like this...
import 'package:flutter/material.dart';
class SomePage extends StatefulWidget {
const SomePage({super.key});
@override
State<SomePage> createState() => _SomePageState();
}
class _SomePageState extends State<SomePage> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
}
void _afterLayout(_) {
print('screen is loaded');
}
@override
Widget build(BuildContext context) {
return Container();
}
}
CodePudding user response:
you want to do something on 'on pressed' just make a void function and call the function in 'inItState' and here you go. you can also manage the conditions when,how or why the function should run.
import 'package:flutter/material.dart';
class SomePage extends StatefulWidget {
const SomePage({super.key});
@override
State<SomePage> createState() => _SomePageState();
}
class _SomePageState extends State<SomePage> {
void yourfunction(){
all the functionalities you want on your button press`enter code here`
}
@override
void initState() {
super.initState();
yourfunction();
}
@override
Widget build(BuildContext context) {
return Scaffold(
child: IconButton(
icon: Icon(
characteristic.isNotifying
? Icons.sync_disabled
: Icons.sync,
color: Theme.of(context).iconTheme.color?.withOpacity(0.5)),
onPressed: yourfunction();
)
);
}
}