赞
踩
flutter 是一个google 的UI框架,它打通了android 和 ios 跨平台的显示上的差异,是一个真正的跨平台开发框架,它不仅仅一套代码 支持生产android 和 ios 同时还可以,支持web,以及PC 端的开发。
UI 组件丰富,风格也漂亮
性能堪比原生
响应式的编程方式,UI 可以实时进行更新,不用重新启动
跨平台支持windows, mac, ios, android ,web 多个端进行同时生成
初始化阶段、运行时阶段、销毁阶段
initState();
didChangeDependencies();
build(); // 在setState 时调用;
didUpdateWidget(); // 在热加载时调用;在父组件发生变化时调用
deactive(); // 该方法在页面退出时调用,是暂时从树上被删除时调用。
dispose(); // 该方法在页面退出时调用,它会从树上永远删除
基类代码
import 'package:flutter/material.dart'; import 'package:logger/logger.dart'; Logger gLogger = Logger(); abstract class BaseWidget extends StatefulWidget { BaseWidget({Key? key}) : super(key: key); late BuildContext mBuildContext; late BaseState mState; setBuildContext(BuildContext context) { mBuildContext = context; } BuildContext getContext() { return mBuildContext; } void setState(VoidCallback fn) { mState.setWidgetState(fn); } // 初始化状态 void initState() {} void didChangeDependencies() {} void didUpdateWidget(covariant BaseWidget oldWidget) {} void dispose() {} // 创建布局 Widget buildLayout(BuildContext context, BaseState state) { mState = state; mBuildContext = context; return build(context); } Widget build(BuildContext context); @override State<StatefulWidget> createState() { return BaseState(); } } class BaseState extends State<BaseWidget> { @override void initState() { super.initState(); widget.initState(); } void setWidgetState(VoidCallback fn) { setState(fn); } @override void didChangeDependencies() { super.didChangeDependencies(); widget.didChangeDependencies(); } @override void didUpdateWidget(covariant BaseWidget oldWidget) { super.didUpdateWidget(oldWidget); widget.didUpdateWidget(oldWidget); } @override void dispose() { super.dispose(); widget.dispose(); } @override Widget build(BuildContext context) { return widget.buildLayout(context, this); } }
使用例子
class HomePage extends BaseWidget { String myTitle = "测试页面"; var _counter = 0; void _incrementCounter() { setState(() { // This call to setState tells the Flutter framework that something has // changed in this State, which causes it to rerun the build method below // so that the display can reflect the updated values. If we changed // _counter without calling setState(), then the build method would not be // called again, and so nothing would appear to happen. _counter++; }); } @override void didUpdateWidget(covariant BaseWidget oldWidget) { // TODO: implement didUpdateWidget super.didUpdateWidget(oldWidget); } @override void didChangeDependencies() { // TODO: implement didChangeDependencies super.didChangeDependencies(); } void page2() { Navigator.push(getContext(), MaterialPageRoute(builder: (context) { return FoundsStore(); })); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(myTitle), backgroundColor: Theme.of(context).colorScheme.inversePrimary, ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.start, children: [ TextButton(onPressed: page2, child: const Text("page2")), Text("value=$_counter"), TextButton(onPressed: _incrementCounter, child: const Text("点击我")) ], ), ), ); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。