rania
rania

Reputation: 73

How to trigger a button action without pressing it with flutter?

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

Upvotes: 1

Views: 1722

Answers (2)

Rana sharjeel Ali
Rana sharjeel Ali

Reputation: 107

If 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();
        )
    );
  }
}

Upvotes: 0

Paulo
Paulo

Reputation: 694

Create a function and just call it on build

Example:

onNotificationPressed(){
  //... do something
}

onNotificationPressed();

Complete sample:

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp( home: MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  static const String _title = 'Flutter - How to trigger a button action without pressing it with flutter?';

  @override
  Widget build(BuildContext context) { 
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatefulWidget(),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({super.key});

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  
  bool isNotifying = true;
  
  void onNotificationPressed(){
    setState(() { isNotifying = !isNotifying; });
  }
  
  @override
  Widget build(BuildContext context) {
    onNotificationPressed();
 
    return IconButton(
            icon: Icon(
                isNotifying
                    ? Icons.sync_disabled
                    : Icons.sync,
                color: Theme.of(context).iconTheme.color?.withOpacity(0.5)),
            onPressed: onNotificationPressed,
          );  
  }
}

You can paste the complete sample on dartpad.dev

Upvotes: 0

Related Questions