在Flutter中添加FloatingActionButton非常容易,但是如果你有一个BottomNavigationBar,并将FloatingActionButton嵌入其中呢,Flutter依然可以搞定,使用floatingActionButtonLocation即可.

FloatingActionButton
FloatingActionButton
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import 'package:flutter/material.dart';

class GoogleFloatingActionButton extends StatefulWidget {
@override
GoogleFloatingActionButtonState createState() =>
new GoogleFloatingActionButtonState();
}

class GoogleFloatingActionButtonState
extends State<GoogleFloatingActionButton> {

List<FloatingActionButtonLocation> _locations;

int _index = 0;

@override
Widget build(BuildContext context) {
return new Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_index++;
if(_index == 4) {
_index = 0;
}
});
},
child: Icon(Icons.add),
foregroundColor: Colors.red,
backgroundColor: Colors.orange,
),

bottomNavigationBar: BottomAppBar(
color: Colors.green,
shape: CircularNotchedRectangle(),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Icon(
Icons.home,
color: Colors.white,
),
Icon(
Icons.person,
color: Colors.white,
),
],
),
),
floatingActionButtonLocation: _locations[_index],
);
}

@override
void initState() {
super.initState();
_locations = [
FloatingActionButtonLocation.centerDocked,
FloatingActionButtonLocation.centerFloat,
FloatingActionButtonLocation.endDocked,
FloatingActionButtonLocation.endFloat,
];
}
}

代码github