Saturday, April 24, 2021

Event handling using flat buttons

 You can surround images or other widgets using FlatButtton and add event handling logic to it using onPressed property and tagging a void callback(without parameters).



import 'package:flutter/material.dart';

void main() {
  return runApp(
    MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.red,
        appBar: AppBar(
          title: Text('Dicee'),
          backgroundColor: Colors.red,
        ),
        body: DicePage(),
      ),
    ),
  );
}

class DicePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: <Widget>[
          Expanded(
            //flex: 4,
            child: FlatButton(
              onPressed:(){//void callback and anonymous function
                print('Left button got pressed');
              } ,
              child: Image(
                //width: 200.0,

                image: AssetImage('images/dice1.png'),
              ),
            ),
          ),
          Expanded(
            //flex: 2,
            child: FlatButton(
              onPressed:()=>print("right button clicked"),
              child: Image.asset(
                'images/dice1.png',
              ),
            ),
          ),
        ],
      ),
    );
  }
}

No comments:

Post a Comment