1+ import 'package:flutter/material.dart' ;
2+
3+ class Index extends StatefulWidget {
4+ @override
5+ _IndexState createState () => _IndexState ();
6+ }
7+
8+ class _IndexState extends State <Index > {
9+ List showMBS = ['Option A' , 'Option B' , 'Option C' ];
10+ String _choiceMBS = 'Nothing' ;
11+ List showBS = ["开始会话" , "操作说明" , "系统设置" , "更多设置" ];
12+ List icons = [Icons .chat, Icons .help, Icons .settings, Icons .more];
13+
14+ Future _openShowModalBottomSheet () async {
15+ final option = await showModalBottomSheet (
16+ context: context,
17+ builder: (BuildContext context) {
18+ return Container (
19+ height: 200.0 ,
20+ child: ListView (
21+ children: List .generate (3 , (index) {
22+ return ListTile (
23+ title: Text (showMBS[index]),
24+ onTap: (){
25+ Navigator .pop (context, showMBS[index]);
26+ },
27+ );
28+ })
29+ ),
30+ );
31+ }
32+ );
33+ // 使用switch可以按情况对后续赋值
34+ setState (() {
35+ _choiceMBS = option;
36+ });
37+ }
38+
39+ Future _openShowBottomSheet () async {
40+ final optionBs = await showBottomSheet (
41+ context: context,
42+ builder: (BuildContext context) {
43+ return new Container (
44+ height: 300.0 ,
45+ child: new Padding (
46+ padding: const EdgeInsets .all (10.0 ),
47+ child: new Column (
48+ children: List .generate (4 , (index) {
49+ return ListTile (
50+ leading: new Icon (icons[index]),
51+ title: new Text (showBS[index]),
52+ onTap: () {
53+ Navigator .pop (context, showBS[index]);
54+ },
55+ );
56+ })
57+ ))
58+ );
59+ }
60+ );
61+ }
62+
63+ @override
64+ Widget build (BuildContext context) {
65+ return Scaffold (
66+ appBar: AppBar (title: Text ('BottomSheet' ),),
67+ body: Center (
68+ child: Column (
69+ mainAxisAlignment: MainAxisAlignment .center,
70+ crossAxisAlignment: CrossAxisAlignment .center,
71+ children: < Widget > [
72+ FlatButton (
73+ onPressed: _openShowBottomSheet,
74+ child: Text ('showBottomSheet' , style: TextStyle (fontSize: 16.0 ),),
75+ ),
76+ FlatButton (
77+ onPressed: _openShowModalBottomSheet,
78+ child: Text ('showModalBottomSheet: $_choiceMBS ' , style: TextStyle (fontSize: 16.0 ),),
79+ )
80+ ],
81+ ),
82+ ),
83+ );
84+ }
85+ }
0 commit comments