main.dart 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. //导入cupertino widget 库
  3. import 'package:flutter/cupertino.dart';
  4. void main() {
  5. runApp(const CupertinoTestRoute());
  6. }
  7. class CupertinoTestRoute extends StatelessWidget {
  8. const CupertinoTestRoute({Key? key}) : super(key: key);
  9. @override
  10. Widget build(BuildContext context) {
  11. return CupertinoPageScaffold(
  12. navigationBar: const CupertinoNavigationBar(
  13. middle: Text("Cupertino Demo"),
  14. ),
  15. child: Center(
  16. child: CupertinoButton(
  17. color: CupertinoColors.activeBlue,
  18. child: const Text("Press"),
  19. onPressed: () {}
  20. ),
  21. ),
  22. );
  23. }
  24. }
  25. */
  26. import 'package:english_words/english_words.dart';
  27. import 'package:flutter/material.dart';
  28. void main() {
  29. runApp(const MyApp());
  30. }
  31. class MyApp extends StatelessWidget {
  32. const MyApp({super.key});
  33. // This widget is the root of your application.
  34. @override
  35. Widget build(BuildContext context) {
  36. return MaterialApp(
  37. title: 'Flutter Demo',
  38. theme: ThemeData(
  39. // This is the theme of your application.
  40. //
  41. // TRY THIS: Try running your application with "flutter run". You'll see
  42. // the application has a blue toolbar. Then, without quitting the app,
  43. // try changing the seedColor in the colorScheme below to Colors.green
  44. // and then invoke "hot reload" (save your changes or press the "hot
  45. // reload" button in a Flutter-supported IDE, or press "r" if you used
  46. // the command line to start the app).
  47. //
  48. // Notice that the counter didn't reset back to zero; the application
  49. // state is not lost during the reload. To reset the state, use hot
  50. // restart instead.
  51. //
  52. // This works for code too, not just values: Most code changes can be
  53. // tested with just a hot reload.
  54. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
  55. useMaterial3: true,
  56. ),
  57. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  58. // home: const CounterWidget(),
  59. // home: const GetStateObjectRoute(),
  60. );
  61. }
  62. }
  63. class MyHomePage extends StatefulWidget {
  64. const MyHomePage({super.key, required this.title});
  65. // This widget is the home page of your application. It is stateful, meaning
  66. // that it has a State object (defined below) that contains fields that affect
  67. // how it looks.
  68. // This class is the configuration for the state. It holds the values (in this
  69. // case the title) provided by the parent (in this case the App widget) and
  70. // used by the build method of the State. Fields in a Widget subclass are
  71. // always marked "final".
  72. final String title;
  73. @override
  74. State<MyHomePage> createState() => _MyHomePageState();
  75. }
  76. class _MyHomePageState extends State<MyHomePage> {
  77. int _counter = 0;
  78. void _incrementCounter() {
  79. setState(() {
  80. // This call to setState tells the Flutter framework that something has
  81. // changed in this State, which causes it to rerun the build method below
  82. // so that the display can reflect the updated values. If we changed
  83. // _counter without calling setState(), then the build method would not be
  84. // called again, and so nothing would appear to happen.
  85. _counter++;
  86. });
  87. //导航到新路由
  88. Navigator.push(
  89. context,
  90. MaterialPageRoute(builder: (context) {
  91. return NewRoute();
  92. }),
  93. );
  94. }
  95. @override
  96. Widget build(BuildContext context) {
  97. // This method is rerun every time setState is called, for instance as done
  98. // by the _incrementCounter method above.
  99. //
  100. // The Flutter framework has been optimized to make rerunning build methods
  101. // fast, so that you can just rebuild anything that needs updating rather
  102. // than having to individually change instances of widgets.
  103. return Scaffold(
  104. appBar: AppBar(
  105. // TRY THIS: Try changing the color here to a specific color (to
  106. // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
  107. // change color while the other colors stay the same.
  108. backgroundColor: Theme.of(context).colorScheme.inversePrimary,
  109. // Here we take the value from the MyHomePage object that was created by
  110. // the App.build method, and use it to set our appbar title.
  111. title: Text(widget.title),
  112. ),
  113. body: Center(
  114. // Center is a layout widget. It takes a single child and positions it
  115. // in the middle of the parent.
  116. // child: Column(
  117. // // Column is also a layout widget. It takes a list of children and
  118. // // arranges them vertically. By default, it sizes itself to fit its
  119. // // children horizontally, and tries to be as tall as its parent.
  120. // //
  121. // // Column has various properties to control how it sizes itself and
  122. // // how it positions its children. Here we use mainAxisAlignment to
  123. // // center the children vertically; the main axis here is the vertical
  124. // // axis because Columns are vertical (the cross axis would be
  125. // // horizontal).
  126. // //
  127. // // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
  128. // // action in the IDE, or press "p" in the console), to see the
  129. // // wireframe for each widget.
  130. // mainAxisAlignment: MainAxisAlignment.center,
  131. // children: <Widget>[
  132. // const Text(
  133. // 'You have pushed the button this many times:',
  134. // ),
  135. // Text(
  136. // '$_counter',
  137. // style: Theme.of(context).textTheme.headlineMedium,
  138. // ),
  139. // ],
  140. // ),
  141. child: Column(
  142. mainAxisAlignment: MainAxisAlignment.center,
  143. children: <Widget>[
  144. RandomWordsWidget(),
  145. ],
  146. ),
  147. ),
  148. floatingActionButton: FloatingActionButton(
  149. onPressed: _incrementCounter,
  150. tooltip: 'Increment',
  151. child: const Icon(Icons.add),
  152. ), // This trailing comma makes auto-formatting nicer for build methods.
  153. );
  154. }
  155. }
  156. // 测试无状态工具描述ui
  157. class Echo extends StatelessWidget {
  158. const Echo({
  159. Key? key,
  160. required this.text,
  161. this.backgroundColor = Colors.grey, //默认为灰色
  162. }) : super(key: key);
  163. final String text;
  164. final Color backgroundColor;
  165. @override
  166. Widget build(BuildContext context) {
  167. return Center(
  168. child: Container(
  169. color: backgroundColor,
  170. child: Text(text),
  171. ),
  172. );
  173. }
  174. }
  175. // 测试State状态生命周期
  176. class CounterWidget extends StatefulWidget {
  177. const CounterWidget({Key? key, this.initValue = 0});
  178. final int initValue;
  179. @override
  180. _CounterWidgetState createState() => _CounterWidgetState();
  181. }
  182. class _CounterWidgetState extends State<CounterWidget> {
  183. int _counter = 0;
  184. @override
  185. void initState() {
  186. super.initState();
  187. //初始化状态
  188. _counter = widget.initValue;
  189. print("initState");
  190. }
  191. @override
  192. Widget build(BuildContext context) {
  193. print("build");
  194. return Scaffold(
  195. body: Center(
  196. child: TextButton(
  197. child: Text('$_counter'),
  198. //点击后计数器自增
  199. onPressed: () => setState(
  200. () => ++_counter,
  201. ),
  202. ),
  203. ),
  204. );
  205. }
  206. @override
  207. void didUpdateWidget(CounterWidget oldWidget) {
  208. super.didUpdateWidget(oldWidget);
  209. print("didUpdateWidget ");
  210. }
  211. @override
  212. void deactivate() {
  213. super.deactivate();
  214. print("deactivate");
  215. }
  216. @override
  217. void dispose() {
  218. super.dispose();
  219. print("dispose");
  220. }
  221. @override
  222. void reassemble() {
  223. super.reassemble();
  224. print("reassemble");
  225. }
  226. @override
  227. void didChangeDependencies() {
  228. super.didChangeDependencies();
  229. print("didChangeDependencies");
  230. }
  231. }
  232. //在 widget 树中获取State对象
  233. class GetStateObjectRoute extends StatefulWidget {
  234. const GetStateObjectRoute({Key? key}) : super(key: key);
  235. @override
  236. State<GetStateObjectRoute> createState() => _GetStateObjectRouteState();
  237. }
  238. class _GetStateObjectRouteState extends State<GetStateObjectRoute> {
  239. @override
  240. Widget build(BuildContext context) {
  241. return Scaffold(
  242. appBar: AppBar(
  243. title: Text("子树中获取State对象"),
  244. ),
  245. body: Center(
  246. child: Column(
  247. children: [
  248. Builder(builder: (context) {
  249. return ElevatedButton(
  250. onPressed: () {
  251. // // 查找父级最近的Scaffold对应的ScaffoldState对象
  252. // ScaffoldState _state = context.findAncestorStateOfType<ScaffoldState>()!;
  253. // // 打开抽屉菜单
  254. // _state.openDrawer();
  255. // // 直接通过of静态方法来获取ScaffoldState
  256. // ScaffoldState _state=Scaffold.of(context);
  257. // // 打开抽屉菜单
  258. // _state.openDrawer();
  259. ScaffoldMessenger.of(context).showSnackBar(
  260. SnackBar(content: Text("我是SnackBar")),
  261. );
  262. },
  263. child: Text('打开抽屉菜单1'),
  264. );
  265. }),
  266. ],
  267. ),
  268. ),
  269. drawer: Drawer(),
  270. );
  271. }
  272. }
  273. // 路由管理,就是页面跳转
  274. class NewRoute extends StatelessWidget {
  275. @override
  276. Widget build(BuildContext context) {
  277. return Scaffold(
  278. appBar: AppBar(
  279. title: Text("New route"),
  280. ),
  281. body: Center(
  282. child: Text("This is new route"),
  283. ),
  284. );
  285. }
  286. }
  287. class RandomWordsWidget extends StatelessWidget {
  288. @override
  289. Widget build(BuildContext context) {
  290. // 生成随机字符串
  291. final wordPair = WordPair.random();
  292. return Padding(
  293. padding: const EdgeInsets.all(8.0),
  294. child: Text(wordPair.toString()),
  295. );
  296. }
  297. }