1- 底部弹窗组件(showModalBottomSheet)

1. 使用 showModalBottom,带阴影的底部弹窗

image.png

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. class CityPage extends StatefulWidget {
  4. @override
  5. _CityPageState createState() => _CityPageState();
  6. }
  7. class _CityPageState extends State<CityPage> {
  8. @override
  9. Widget build(BuildContext context) {
  10. return Scaffold(
  11. appBar: AppBar(
  12. title: Text("带阴影的底部弹出框使用"),
  13. centerTitle: true,
  14. ),
  15. body: FlatButton(
  16. onPressed: () {
  17. // 带阴影的底部弹窗框
  18. showModalBottomSheet(
  19. context: context,
  20. builder: (BuildContext context) {
  21. return Container(
  22. height: 300,
  23. );
  24. },
  25. );
  26. },
  27. child: Text("bottom show modal"),
  28. color: Colors.red,
  29. ),
  30. );
  31. }
  32. }

2. 添加取消,确定按钮,点按钮可关闭弹窗

image.png

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. class CityPage extends StatefulWidget {
  4. @override
  5. _CityPageState createState() => _CityPageState();
  6. }
  7. class _CityPageState extends State<CityPage> {
  8. @override
  9. Widget build(BuildContext context) {
  10. return Scaffold(
  11. appBar: AppBar(
  12. title: Text("带阴影的底部弹出框使用"),
  13. centerTitle: true,
  14. ),
  15. body: FlatButton(
  16. onPressed: () {
  17. showModalBottomSheet(
  18. context: context,
  19. builder: (BuildContext context) {
  20. return Container(
  21. height: 300,
  22. child: Column(
  23. children: [
  24. // 添加取消,确定按钮
  25. Row(
  26. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  27. children: [
  28. FlatButton(
  29. onPressed: () {
  30. // 点击关闭
  31. Navigator.of(context).pop();
  32. },
  33. child: Text("取消")),
  34. FlatButton(
  35. onPressed: () {
  36. // 点击关闭
  37. Navigator.of(context).pop();
  38. },
  39. child: Text("确定")),
  40. ],
  41. )
  42. ],
  43. ),
  44. );
  45. },
  46. );
  47. },
  48. child: Text("bottom show modal"),
  49. color: Colors.red,
  50. ),
  51. );
  52. }
  53. }

2- 底部弹窗组件(showBottomSheet)

image.png

1. showBottomSheet 没有黑色蒙层的弹窗

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. class CityPage extends StatefulWidget {
  4. @override
  5. _CityPageState createState() => _CityPageState();
  6. }
  7. class _CityPageState extends State<CityPage> {
  8. // 先定义key
  9. final _key = GlobalKey<ScaffoldState>();
  10. // 打开底部弹窗
  11. _openBottomSheet() {
  12. _key.currentState.showBottomSheet(
  13. (context) => Container(
  14. height: 300,
  15. color: Colors.grey,
  16. ),
  17. );
  18. }
  19. @override
  20. Widget build(BuildContext context) {
  21. return Scaffold(
  22. key: _key,
  23. appBar: AppBar(
  24. title: Text("带阴影的底部弹出框使用"),
  25. centerTitle: true,
  26. ),
  27. body: FlatButton(
  28. onPressed: _openBottomSheet,
  29. child: Text("bottom show modal"),
  30. color: Colors.green,
  31. ),
  32. );
  33. }
  34. }

image.png

2.使用 Navigator.of(context).pop() 关闭弹窗

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. class CityPage extends StatefulWidget {
  4. @override
  5. _CityPageState createState() => _CityPageState();
  6. }
  7. class _CityPageState extends State<CityPage> {
  8. // 定义key
  9. final _key = GlobalKey<ScaffoldState>();
  10. // 打开底部弹窗
  11. _openBottomSheet() {
  12. _key.currentState.showBottomSheet(
  13. (context) => Container(
  14. height: 300,
  15. color: Colors.grey,
  16. child: Column(
  17. children: [
  18. Row(
  19. children: [
  20. FlatButton(
  21. // 关闭底部弹窗
  22. onPressed: () {
  23. Navigator.of(context).pop();
  24. },
  25. child: Text("关闭"),
  26. )
  27. ],
  28. )
  29. ],
  30. ),
  31. ),
  32. );
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. return Scaffold(
  37. key: _key,
  38. appBar: AppBar(
  39. title: Text("带阴影的底部弹出框使用"),
  40. centerTitle: true,
  41. ),
  42. body: FlatButton(
  43. onPressed: _openBottomSheet,
  44. child: Text("bottom show modal"),
  45. color: Colors.green,
  46. ),
  47. );
  48. }
  49. }

3. ios风格的底部滚动列表

image.png

1. 使用 CupertinoPicker 组件,实现滚动

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. class CityPage extends StatefulWidget {
  4. @override
  5. _CityPageState createState() => _CityPageState();
  6. }
  7. class _CityPageState extends State<CityPage> {
  8. // 第一行的左右按钮
  9. _oneRowButton() {
  10. return Row(
  11. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  12. children: [
  13. FlatButton(
  14. onPressed: () {
  15. Navigator.of(context).pop();
  16. },
  17. child: Text("取消"),
  18. ),
  19. FlatButton(
  20. onPressed: () {
  21. Navigator.of(context).pop();
  22. },
  23. child: Text("确定"),
  24. )
  25. ],
  26. );
  27. }
  28. // 打开底部弹窗
  29. _openBottomSheet() {
  30. showModalBottomSheet(
  31. context: context,
  32. builder: (BuildContext context) {
  33. return Container(
  34. height: 320,
  35. child: Column(
  36. children: [
  37. _oneRowButton(),
  38. // 底部的ios风格的滚动列表
  39. _iosWheelWidget(),
  40. ],
  41. ),
  42. );
  43. },
  44. );
  45. }
  46. // 滚动选择列表
  47. _iosWheelWidget() {
  48. return Expanded(
  49. child: CupertinoPicker(
  50. itemExtent: 35,
  51. onSelectedItemChanged: (int index) {},
  52. children: [
  53. Text("1年工作经验"),
  54. Text("2年工作经验"),
  55. Text("3年工作经验"),
  56. Text("4年工作经验"),
  57. Text("5年工作经验"),
  58. ],
  59. ),
  60. );
  61. }
  62. @override
  63. Widget build(BuildContext context) {
  64. return Scaffold(
  65. appBar: AppBar(
  66. title: Text("带阴影的底部弹出框使用"),
  67. centerTitle: true,
  68. ),
  69. body: FlatButton(
  70. onPressed: _openBottomSheet,
  71. child: Text("ios底部滚动列表"),
  72. color: Colors.green,
  73. ),
  74. );
  75. }
  76. }

2. CupertinoPicker 组件的常用参数配置说明

可选,必选参数名 参数传值说明
itemExtent 每个子项高度
children 子widget组。List
onSelectedItemChanged 滚动选择的回调,每次滚动,都会触发此回调,会将选中的widget的 索引 返回
useMagnifier 是否开启选中项的字体放大功能,默认false
magnification 选中的元素,放大的倍数,在useMagnifier:true 的情况下有效
backgroundColor 给CupertinoPicker容器添加 背景颜色
offAxisFraction 控制选中的widget元素的左右偏移量,默认是0.0。,正数向右偏移,负数向左偏移
scrollController 控制器,通过 FixedExtentScrollController()创建,可以实现默认选中的哪一个

3. 给 CupertinoPicker 设置默认选中的值

  • 默认选中第3个元素

image.png

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. class CityPage extends StatefulWidget {
  4. @override
  5. _CityPageState createState() => _CityPageState();
  6. }
  7. class _CityPageState extends State<CityPage> {
  8. // 1. 创建 控制器
  9. FixedExtentScrollController _controller;
  10. @override
  11. initState() {
  12. super.initState();
  13. // 控制器实例化, 传入默认选中的索引
  14. _controller = FixedExtentScrollController(initialItem: 2);
  15. }
  16. // 第一行的左右按钮
  17. _oneRowButton() {
  18. return Row(
  19. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  20. children: [
  21. FlatButton(
  22. onPressed: () {
  23. Navigator.of(context).pop();
  24. },
  25. child: Text("取消"),
  26. ),
  27. FlatButton(
  28. onPressed: () {
  29. Navigator.of(context).pop();
  30. },
  31. child: Text("确定"),
  32. )
  33. ],
  34. );
  35. }
  36. // 打开底部弹窗
  37. _openBottomSheet() {
  38. showModalBottomSheet(
  39. context: context,
  40. builder: (BuildContext context) {
  41. return Container(
  42. height: 320,
  43. child: Column(
  44. children: [
  45. _oneRowButton(),
  46. // 底部的ios风格的滚动列表
  47. _iosWheelWidget(),
  48. ],
  49. ),
  50. );
  51. },
  52. );
  53. }
  54. // 滚动选择列表
  55. _iosWheelWidget() {
  56. return Expanded(
  57. child: CupertinoPicker(
  58. itemExtent: 35,
  59. scrollController: _controller, // 绑定控制器
  60. onSelectedItemChanged: (int index) {},
  61. children: [
  62. Text("1年工作经验"),
  63. Text("2年工作经验"),
  64. Text("3年工作经验"),
  65. Text("4年工作经验"),
  66. Text("5年工作经验"),
  67. ],
  68. ),
  69. );
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. return Scaffold(
  74. appBar: AppBar(
  75. title: Text("带阴影的底部弹出框使用"),
  76. centerTitle: true,
  77. ),
  78. body: FlatButton(
  79. onPressed: _openBottomSheet,
  80. child: Text("ios底部滚动列表"),
  81. color: Colors.green,
  82. ),
  83. );
  84. }
  85. }