GestureDetector 與 Button

Button 有 ElevatedButton,TextButton,OutlineButton,FloatingActionButton, IconButton, Switch,Checkbox。

都是用onPressed 屬性接收點擊事件。

語法:

onPressed: 匿名函式
onPressed:() {}

TextButton()

TextButton沒有自己的寬高,必須包在Container父元件,因為Container有寬高。

方法 說明
onPressed 點一下

語法:

onPressed:() {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import 'package:flutter/material.dart';

void main() {
  runApp(MainPage());
}

class MainPage extends StatefulWidget {
  MainPage({Key? key}) : super(key: key);

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Container(
      color: Colors.yellow,
      child: Container(
          width: 50.0,
          height: 50.0,
          color: Colors.red,
          child: TextButton(
              onPressed: () {
                print("abcd");
              },
              child: Text("送出"))),
    )));
  }
}

GestureDetector

方法 說明
onTap 點一下
onDoubleTap 點二下

語法:

onTap: () {
  print("GestureDetector");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import 'package:flutter/material.dart';

void main() {
  runApp(MainPage());
}

class MainPage extends StatefulWidget {
  MainPage({Key? key}) : super(key: key);

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  TextEditingController _controller = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: Container(
      color: Colors.yellow,
      child: Container(
          width: 50.0,
          height: 50.0,
          color: Colors.red,
          child: GestureDetector(
              onTap: () {
                print("GestureDetector");
              },
              onDoubleTap: () {
                print("doubleTap");
              },
              child: Text("送出"))),
    )));
  }

results matching ""

    No results matching ""