今回は「ElevatedButton」の基本的な使い方について紹介します。
ElevatedButtonを使えばCardなどを使わずに立体的なボタンを作れます!
目次
ElevatedButtonの基本プロパティ
- child(必須)
- onPressed(必須)
- onLongPress
- style
child & onPressed(必須)
childプロパティでボタンのラベル、onPressedプロパティでボタンがクリックされた際のVoidCallbackを指定します。
//ソース
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Button'),
),
body: Center(
child: ElevatedButton(
child: Text(
'ElevatedButton',
style: TextStyle(fontSize: 20),
),
onPressed: () {
print('クリックされました');
},
onLongPress: () {
print('長押しされました');
},
),
),
),
);
}
onLongPress
onLongPressプロパティで長押しされた際のVoidCallbackを指定できます。
style

styleプロパティでElevatedButtonのstaticメソッド「styleFrom」を使用してボタンの見た目を変更できます。
//ソース
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Button'),
),
body: Center(
child: ElevatedButton(
child: Text(
'ElevatedButton',
style: TextStyle(fontSize: 20),
),
style: ElevatedButton.styleFrom(
primary: Colors.green,
),
onPressed: () {
print('クリックされました');
},
onLongPress: () {
print('長押しされました');
},
),
),
),
);
}
以上です。