How To Set Onclick Onpress Event On Raised Button Using Flutter

admin_img Posted By Bajarangi soft , Posted On 13-10-2020

Raised Button is our main widget to create buttons in Flutter. Raised button has its own onClick method known as onPressed. We can pass any function here in onPressed event and that particular function will called on button onPress event. We cannot set onPressed empty we should have pass something here like a function our inline function body code.

How To Set Onclick Onpress Event On Raised Button Using Flutter

Onclick Onpress Event On Raised Button
Complete Code For Onclick Onpress Event On Raised Button In Flutter
Main.dart

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  sampleFunction(){
    print('Function On Click Event Called.');
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
        home: Scaffold(
          appBar: AppBar(
            backgroundColor: Color(0xFF4A148C),
            title: Text('Onclick Onpress Event'),
            centerTitle: true,
          ),
            body: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children:[
                    RaisedButton(
                      child: Text(" Button With OnPress "),
                      onPressed: sampleFunction,
                      color: Color(0xFF4A148C),
                      textColor: Colors.white,
                      splashColor: Colors.grey,
                      padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                    )

                  ],
                )
            )
        )
    );
  }
}

Related Post