23Flutter FloatingActionButton实现类似闲鱼App底部导航凸起按钮:

冷不防 2023-08-17 15:59 184阅读 0赞
  1. /*
  2. 一、Flutter FloatingActionButton介绍
  3. FloatingActionButton简称FAB,可以实现浮动按钮,也可以实现类型闲鱼app的底部凸起导航。
  4. child:子视图,一般为Icon,不推荐使用文字。
  5. tooltip:FAB被长按时显示,也是无障碍功能
  6. backgroundColor:背景颜色
  7. elevation:未点击的时候的阴影
  8. hignlightElevation:点击时阴影值,默认12.0
  9. onPressed:点击事件回调
  10. shape:可以定义FAB的形状等。
  11. mini:是否是mini类型默认false
  12. */

Tabs.dart【设置闲鱼APP凸起按钮方法】

  1. import 'package:flutter/material.dart';
  2. import 'tabs/Home.dart';
  3. import 'tabs/Category.dart';
  4. import 'tabs/Setting.dart';
  5. class Tabs extends StatefulWidget {
  6. final index;
  7. Tabs({Key key, this.index = 1}) : super(key: key);
  8. _TabsState createState() => _TabsState(this.index);
  9. }
  10. class _TabsState extends State<Tabs> {
  11. int _currentIndex = 0;
  12. _TabsState(index) {
  13. this._currentIndex = index;
  14. }
  15. List _pageList = [HomePage(), CategoryPage(), SettingPage()];
  16. @override
  17. Widget build(BuildContext context) {
  18. return Scaffold(
  19. appBar: AppBar(
  20. title: Text('Flutter Demo'),
  21. ),
  22. floatingActionButton: Container(
  23. height: 80,
  24. width: 80,
  25. padding: EdgeInsets.all(8),
  26. margin: EdgeInsets.only(top: 10),
  27. decoration: BoxDecoration(
  28. borderRadius: BorderRadius.circular(40),
  29. color: Colors.white
  30. ),
  31. child: FloatingActionButton(
  32. child: Icon(Icons.add),
  33. onPressed: () {
  34. // print("floatingActionButton tabs");
  35. setState(() {
  36. this._currentIndex=1;
  37. });
  38. },
  39. backgroundColor: this._currentIndex==1?Colors.red:Colors.yellow,
  40. ),
  41. ),
  42. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  43. bottomNavigationBar: BottomNavigationBar(
  44. currentIndex: this._currentIndex,
  45. onTap: (int index) {
  46. // print(index);
  47. setState(() {
  48. this._currentIndex = index;
  49. });
  50. },
  51. iconSize: 36.0,
  52. type: BottomNavigationBarType.fixed,
  53. fixedColor: Colors.red,
  54. items: [
  55. BottomNavigationBarItem(icon: Icon(Icons.home), title: Text('首页')),
  56. BottomNavigationBarItem(
  57. icon: Icon(Icons.category), title: Text('分类')),
  58. BottomNavigationBarItem(
  59. icon: Icon(Icons.settings), title: Text('设置')),
  60. ]),
  61. body: this._pageList[this._currentIndex],
  62. drawer: Drawer(
  63. child: Column(
  64. children: <Widget>[
  65. Row(
  66. children: <Widget>[
  67. // Expanded(
  68. // child: DrawerHeader(
  69. // child: Text('你好Flutter'),
  70. // decoration: BoxDecoration(
  71. // // color: Colors.yellow
  72. // image: DecorationImage(
  73. // image: NetworkImage('https://www.itying.com/images/flutter/2.png'),
  74. // fit:BoxFit.cover
  75. // ),
  76. // ),
  77. // )
  78. // )
  79. Expanded(
  80. child: UserAccountsDrawerHeader(
  81. accountName: Text('老师你好'),
  82. accountEmail: Text('gztt@163.com'),
  83. currentAccountPicture: CircleAvatar(
  84. backgroundImage: NetworkImage(
  85. 'https://www.itying.com/images/flutter/3.png'),
  86. ),
  87. decoration: BoxDecoration(
  88. // color: Colors.yellow
  89. image: DecorationImage(
  90. image: NetworkImage(
  91. 'https://www.itying.com/images/flutter/2.png'),
  92. fit: BoxFit.cover),
  93. ),
  94. otherAccountsPictures: <Widget>[
  95. Image.network(
  96. 'https://www.itying.com/images/flutter/5.png'),
  97. Image.network(
  98. 'https://www.itying.com/images/flutter/4.png')
  99. ],
  100. ),
  101. )
  102. ],
  103. ),
  104. ListTile(
  105. leading: CircleAvatar(
  106. child: Icon(Icons.home),
  107. ),
  108. title: Text('我的空间')),
  109. Divider(),
  110. ListTile(
  111. leading: CircleAvatar(
  112. child: Icon(Icons.home),
  113. ),
  114. title: Text('用户中心'),
  115. onTap: () {
  116. Navigator.of(context).pop();
  117. Navigator.pushNamed(context, '/user');
  118. }),
  119. Divider(),
  120. ListTile(
  121. leading: CircleAvatar(
  122. child: Icon(Icons.home),
  123. ),
  124. title: Text('用户中心'),
  125. )
  126. ],
  127. ),
  128. ),
  129. endDrawer: Drawer(
  130. child: Text('右侧侧边栏'),
  131. ),
  132. );
  133. }
  134. }

Button.dart

  1. import 'package:flutter/material.dart';
  2. class ButtonDemoPage extends StatelessWidget {
  3. const ButtonDemoPage({Key key}) : super(key: key);
  4. @override
  5. Widget build(BuildContext context) {
  6. return Scaffold(
  7. appBar: AppBar(
  8. title: Text('按钮演示页面'),
  9. actions: <Widget>[
  10. IconButton(
  11. icon: Icon(Icons.settings),
  12. onPressed: () {},
  13. )
  14. ],
  15. ),
  16. floatingActionButton: FloatingActionButton(
  17. child: Icon(Icons.add,color: Colors.black,size: 40),
  18. onPressed: (){
  19. print("floatingActionButton");
  20. },
  21. backgroundColor: Colors.yellow,
  22. ),
  23. floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
  24. body: Column(
  25. mainAxisAlignment: MainAxisAlignment.center,
  26. children: <Widget>[
  27. Row(
  28. children: <Widget>[
  29. RaisedButton(
  30. child: Text('普通'),
  31. onPressed: () {
  32. print('普通按钮');
  33. },
  34. ),
  35. SizedBox(width: 5),
  36. RaisedButton.icon(
  37. icon: Icon(Icons.search),
  38. label: Text('图标'),
  39. onPressed: null,
  40. ),
  41. SizedBox(width: 10),
  42. RaisedButton(
  43. child: Text('有颜色'),
  44. color: Colors.blue,
  45. textColor: Colors.white,
  46. onPressed: () {
  47. print('有颜色按钮');
  48. },
  49. ),
  50. SizedBox(width: 10),
  51. RaisedButton(
  52. child: Text('有阴影'),
  53. color: Colors.blue,
  54. textColor: Colors.white,
  55. elevation: 10,
  56. onPressed: () {
  57. print('有阴影按钮');
  58. }),
  59. ],
  60. ),
  61. SizedBox(height: 10),
  62. Row(
  63. mainAxisAlignment: MainAxisAlignment.center,
  64. children: <Widget>[
  65. Container(
  66. height: 50,
  67. width: 400,
  68. child: RaisedButton(
  69. child: Text('宽度高度'),
  70. color: Colors.blue,
  71. textColor: Colors.white,
  72. elevation: 20,
  73. onPressed: () {},
  74. ),
  75. )
  76. ],
  77. ),
  78. SizedBox(height: 10),
  79. Row(
  80. mainAxisAlignment: MainAxisAlignment.center,
  81. children: <Widget>[
  82. Expanded(
  83. child: Container(
  84. height: 60,
  85. margin: EdgeInsets.all(10),
  86. child: RaisedButton(
  87. child: Text('自适应按钮'),
  88. color: Colors.blue,
  89. textColor: Colors.white,
  90. elevation: 20,
  91. onPressed: () {
  92. print('自适应按钮');
  93. },
  94. ),
  95. ))
  96. ],
  97. ),
  98. SizedBox(height: 10),
  99. Row(
  100. mainAxisAlignment: MainAxisAlignment.center,
  101. children: <Widget>[
  102. RaisedButton(
  103. child: Text('圆角按钮'),
  104. color: Colors.blue,
  105. textColor: Colors.white,
  106. elevation: 20,
  107. shape: RoundedRectangleBorder(
  108. borderRadius: BorderRadius.circular(10)),
  109. onPressed: () {
  110. print('圆角按钮');
  111. },
  112. ),
  113. RaisedButton(
  114. child: Text('圆形按钮'),
  115. color: Colors.blue,
  116. textColor: Colors.white,
  117. elevation: 20,
  118. splashColor: Colors.grey,
  119. shape: CircleBorder(side: BorderSide(color: Colors.white)),
  120. onPressed: () {
  121. print('圆形按钮');
  122. },
  123. )
  124. ],
  125. ),
  126. FlatButton(
  127. //扁平化按钮:
  128. child: Text('扁平化的按钮'),
  129. color: Colors.blue,
  130. textColor: Colors.yellow,
  131. onPressed: () {},
  132. ),
  133. OutlineButton(
  134. child: Text('线框按钮'),
  135. onPressed: () {
  136. print('OutlineButton');
  137. },
  138. ),
  139. Row(
  140. mainAxisAlignment: MainAxisAlignment.center,
  141. children: <Widget>[
  142. Expanded(
  143. child: Container(
  144. margin: EdgeInsets.all(20),
  145. height: 50,
  146. child: OutlineButton(
  147. child: Text('注册'),
  148. onPressed: () {},
  149. ),
  150. ),
  151. )
  152. ],
  153. ),
  154. Row(
  155. mainAxisAlignment: MainAxisAlignment.center,
  156. children: <Widget>[
  157. ButtonBar(
  158. //按钮组
  159. children: <Widget>[
  160. RaisedButton(
  161. child: Text('登录'),
  162. color: Colors.blue,
  163. textColor: Colors.white,
  164. onPressed: () {},
  165. ),
  166. RaisedButton(
  167. child: Text('注册'),
  168. color: Colors.blue,
  169. textColor: Colors.white,
  170. onPressed: () {},
  171. ),
  172. ],
  173. )
  174. ],
  175. ),
  176. MyButton(
  177. text: "自定义按钮",
  178. height: 60.0,
  179. width: 100.0,
  180. pressed: () {
  181. print("自定义按钮");
  182. },
  183. )
  184. ],
  185. ));
  186. }
  187. }
  188. //自定义按钮组件:
  189. class MyButton extends StatelessWidget {
  190. final text;
  191. final pressed;
  192. final double width;
  193. final double height;
  194. const MyButton(
  195. {
  196. this.text = '', this.pressed = null, this.width = 80, this.height = 30});
  197. @override
  198. Widget build(BuildContext context) {
  199. return Container(
  200. height: this.height,
  201. width: this.width,
  202. child: RaisedButton(
  203. child: Text(this.text),
  204. onPressed: this.pressed,
  205. ),
  206. );
  207. }
  208. }

转载于:https://www.cnblogs.com/yiweiyihang/p/11512814.html

发表评论

表情:
评论列表 (有 0 条评论,184人围观)

还没有评论,来说两句吧...

相关阅读