【笔记】Flutter的按钮组件

前言

Flutter的按钮组件学习笔记

按钮组件

普通按钮

onPressed:按钮事件

() {}:定义事件的函数,如果传递空函数表示没有事件但是可以被点击
null:如果传递null按钮会显示灰色,不可被点击

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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [
App()
],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
...
},
child: const Text("文本内容"),
);
}
}

带图片的普通按钮

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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [App()],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return ElevatedButton.icon(
onPressed: () {},
icon: const Icon(Icons.home),
label: const Text("文本内容"),
);
}
}

按钮颜色

backgroundColor:设置背景颜色
foregroundColor:文字颜色

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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [App()],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {},
child: const Text("文本内容"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.white),
foregroundColor: MaterialStateProperty.all(Colors.black),
),
);
}
}

按钮宽高

  • 按钮会铺满SizeBox,通过设置SizeBox的宽高改变按钮的宽高
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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [App()],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return SizedBox(
width: 200,
height: 200,
child: ElevatedButton(
onPressed: () {},
child: const Text("文本内容"),
),
);
}
}

圆角按钮

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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [App()],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {},
child: const Text("文本内容"),
style: ButtonStyle(
shape: MaterialStateProperty.all(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)
)
)
),
);
}
}

圆形按钮

color:指定边框颜色

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(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [App()],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {},
child: const Text("文本内容"),
style: ButtonStyle(
shape: MaterialStateProperty.all(
CircleBorder(
side: BorderSide(
color: Colors.yellow
)
)
)
),
);
}
}

文本按钮

  • 类似于超链接
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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [
App()
],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return TextButton(
onPressed: () {},
child: const Text("文本内容"),
);
}
}

带图片的文本按钮

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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [App()],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return TextButton.icon(
onPressed: () {},
icon: const Icon(Icons.home),
label: const Text("文本内容"),
);
}
}

带边框的按钮

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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [
App()
],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return OutlinedButton(
onPressed: () {},
child: const Text("文本内容"),
);
}
}

带图片的带边框的按钮

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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [App()],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return OutlinedButton.icon(
onPressed: () {},
icon: const Icon(Icons.home),
label: const Text("文本内容"),
);
}
}

配置边框

width:边框宽度
color:边框颜色

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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [App()],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return OutlinedButton(
onPressed: () {},
child: const Text("文本内容"),
style: ButtonStyle(
side: MaterialStateProperty.all(
BorderSide(
width: 1,
color: Colors.red,
)
)
),
);
}
}

图片按钮

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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [
App()
],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return IconButton(
onPressed: () {},
icon: const Icon(Icons.home),
);
}
}

浮动按钮

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
import 'package:flutter/material.dart';

void main() {
runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("文本内容"),
),
body: Column(
children: const [
App()
],
),
),
));
}

class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: const Icon(Icons.add),
),
);
}
}

完成

参考文献

哔哩哔哩——筱筱知晓