18 Flutter: RaisedButton with parameters

main.dart
import 'package:flutter/material.dart';

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

class MyApp extends StatefulWidget {
  @override
  _State createState() => new _State();
}

class _State extends State<MyApp>{

  String _value = 'Hello World';

  void _onPressed(String value) {
    setState(() {
      _value = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Name here'),
      ),
      //hit Ctrl+space in intellij to know what are the options you can use in flutter widgets
      body: new Container(
        padding: new EdgeInsets.all(32.0),
        child: new Center(
          child: new Column(
            children: <Widget>[
              new Text(_value),
              //dart treates everything as objects so we pass a function in onPressed value
              new RaisedButton(onPressed: () => _onPressed('Hello Raunak'), child: new Text('Click me')),
            ],
          ),
        ),
      ),
    );
  }
}

Last updated