- 創(chuàng)建一個(gè)按鈕
- 點(diǎn)擊事件
創(chuàng)建一個(gè)按鈕
話不多說,起手?jǐn)]一個(gè)按鈕,然后名字設(shè)置為按鈕
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'belineApp',
home: Scaffold(
appBar: AppBar(title: Text('beline App')),
body: Center(
child: ListView(
children: <Widget>[
RaisedButton(
child: Text('按鈕'),
onPressed: () {},
)
],
),
)
),
);
}
}
點(diǎn)擊事件
RaisedButton
組件在Flutter是一個(gè)按鈕組件,通過調(diào)用onPressed
方法來觸發(fā)點(diǎn)擊事件.做一個(gè)簡單的計(jì)數(shù)器:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget{
var num = 0;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'belineApp',
home: Scaffold(
appBar: AppBar(title: Text('beline App')),
body: Center(
child: ListView(
children: <Widget>[
RaisedButton(
child: Text('計(jì)數(shù)器'),
onPressed: () {
print(this.num += 1);
},
)
],
),
)
),
);
}
}
此時(shí),每次點(diǎn)擊一次按鈕,都會在控制臺上輸出一次點(diǎn)擊次數(shù)
image.png