|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | + |
| 3 | +enum Commands { heroAndScholar, hurricaneCame } |
| 4 | + |
| 5 | +/// |
| 6 | +/// DropDownPage |
| 7 | +class DropDownPage extends StatefulWidget { |
| 8 | + @override |
| 9 | + State<StatefulWidget> createState() { |
| 10 | + return _DropState(); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +/// |
| 15 | +/// _DropState |
| 16 | +class _DropState extends State<DropDownPage> { |
| 17 | + String dropdownValue = 'One'; |
| 18 | + |
| 19 | + //实例变量(非静态变量)可以是final但不能是const |
| 20 | + //const不能修饰实例变量 |
| 21 | + //final可以修饰实例变量 |
| 22 | + |
| 23 | + //const不仅能修饰'类变量',还可以修饰'变量值'; |
| 24 | + //特殊:constVar 可以修改值(const [1,2,3]此时不可被修改值,也就是等同于是'数组') |
| 25 | + var constVar = const [1, 2, 3]; |
| 26 | + |
| 27 | + //特殊:pra 不可以修改值 |
| 28 | + static const pra = ""; |
| 29 | + |
| 30 | + final String parame = ""; |
| 31 | + |
| 32 | + List<String> _dropDownItems = [ |
| 33 | + 'One', |
| 34 | + 'Two', |
| 35 | + 'Free', |
| 36 | + 'Four', |
| 37 | + ]; |
| 38 | + |
| 39 | + bool _heroAndScholar = false; |
| 40 | + |
| 41 | + @override |
| 42 | + Widget build(BuildContext context) { |
| 43 | + //constVar.add(5);//Error -> 'Unsupported operation: Cannot add to an unmodifiable list' |
| 44 | + constVar = [1, 23]; |
| 45 | + constVar.sort(); |
| 46 | + |
| 47 | + return Scaffold( |
| 48 | + appBar: AppBar( |
| 49 | + title: Text("DropDown"), |
| 50 | + ), |
| 51 | + body: Row( |
| 52 | + children: <Widget>[ |
| 53 | + Spacer(), |
| 54 | + Column( |
| 55 | + children: <Widget>[ |
| 56 | + Spacer(), |
| 57 | + DropdownButton<String>( |
| 58 | + isExpanded: false, |
| 59 | + isDense: false, |
| 60 | + value: dropdownValue, |
| 61 | + onChanged: (String newValue) { |
| 62 | + setState(() { |
| 63 | + dropdownValue = newValue; |
| 64 | + }); |
| 65 | + }, |
| 66 | + icon: Icon(Icons.arrow_downward), |
| 67 | + iconSize: 24, |
| 68 | + elevation: 2, |
| 69 | + style: TextStyle(color: Colors.deepPurple), |
| 70 | + underline: Container( |
| 71 | + height: 2, |
| 72 | + color: Colors.deepPurpleAccent, |
| 73 | + ), |
| 74 | + selectedItemBuilder: (BuildContext context) { |
| 75 | + return _dropDownItems.map<Widget>((String item) { |
| 76 | + return Container( |
| 77 | + alignment: Alignment.center, |
| 78 | + child: Text(item), |
| 79 | + ); |
| 80 | + }).toList(); |
| 81 | + }, |
| 82 | + items: _dropDownItems |
| 83 | + .map<DropdownMenuItem<String>>((String value) { |
| 84 | + return DropdownMenuItem<String>( |
| 85 | + value: value, |
| 86 | + child: Text(value), |
| 87 | + ); |
| 88 | + }).toList(), |
| 89 | + ), |
| 90 | + Spacer(), |
| 91 | + PopupMenuButton<Commands>( |
| 92 | + onSelected: (Commands result) { |
| 93 | + switch (result) { |
| 94 | + case Commands.heroAndScholar: |
| 95 | + setState(() { |
| 96 | + _heroAndScholar = !_heroAndScholar; |
| 97 | + }); |
| 98 | + break; |
| 99 | + case Commands.hurricaneCame: |
| 100 | + // ...handle hurricane option |
| 101 | + |
| 102 | + break; |
| 103 | + // ...other items handled here |
| 104 | + } |
| 105 | + }, |
| 106 | + itemBuilder: (BuildContext context) => |
| 107 | + <PopupMenuEntry<Commands>>[ |
| 108 | + CheckedPopupMenuItem<Commands>( |
| 109 | + checked: _heroAndScholar, |
| 110 | + value: Commands.heroAndScholar, |
| 111 | + child: const Text('Hero and scholar'), |
| 112 | + ), |
| 113 | + const PopupMenuDivider(), |
| 114 | + const PopupMenuItem<Commands>( |
| 115 | + value: Commands.hurricaneCame, |
| 116 | + child: ListTile( |
| 117 | + leading: Icon(null), |
| 118 | + title: Text('Bring hurricane'), |
| 119 | + ), |
| 120 | + ), |
| 121 | + ], |
| 122 | + ), |
| 123 | + MaterialButton( |
| 124 | + onPressed: () { |
| 125 | + print("first"); |
| 126 | + //future |
| 127 | + getUserInfo().then((value) { |
| 128 | + print(value); |
| 129 | + }).catchError((e) { |
| 130 | + print(e); |
| 131 | + }); |
| 132 | + |
| 133 | + print("last"); |
| 134 | + }, |
| 135 | + child: Text("Click by future"), |
| 136 | + ), |
| 137 | + MaterialButton( |
| 138 | + onPressed: () async { |
| 139 | + print("first"); |
| 140 | + |
| 141 | + //async await |
| 142 | + try { |
| 143 | + print(await getUserInfo()); |
| 144 | + } catch (e) { |
| 145 | + print(e); |
| 146 | + } finally { |
| 147 | + print("finally"); |
| 148 | + } |
| 149 | + |
| 150 | + print("last"); |
| 151 | + }, |
| 152 | + child: Text("Click by async"), |
| 153 | + ), |
| 154 | + Spacer(), |
| 155 | + Spacer(), |
| 156 | + Spacer(), |
| 157 | + ], |
| 158 | + ), |
| 159 | + Spacer(), |
| 160 | + ], |
| 161 | + ), |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | +// Future<String> getLoginUser() async { |
| 166 | +// return await Future<String>.delayed( |
| 167 | +// Duration(seconds: 5), |
| 168 | +// () { |
| 169 | +// return ""; |
| 170 | +// }, |
| 171 | +// ); |
| 172 | +// } |
| 173 | + |
| 174 | + Future<String> getUserInfo() { |
| 175 | + return Future<String>.delayed(Duration(seconds: 5), () { |
| 176 | + return "delayed task"; |
| 177 | + }); |
| 178 | + } |
| 179 | +} |
0 commit comments