栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Flutter上的小部件的onResume()和onPause()

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Flutter上的小部件的onResume()和onPause()

最常见的情况是,如果您正在运行动画并且不想在后台消耗资源。在这种情况下,你应该延长您的

State
使用
TickerProviderStateMixin
,并使用你的
State
作为
vsync
的说法
AnimationController
。当您
State
可见时,
Flutter
只会处理动画控制器的侦听器。

如果你想

State
住在你的S PageRoute当被置于PageRoute被其他内容遮挡,可以传递的参数,以您的构造函数。如果执行此操作,则隐藏时将重置自身(及其子代),并且必须使用作为 其构造函数参数传入的属性来 重新构造自身。如果您不希望 完全重置 ,则可以使用模型或控制器类或来保存用户的进度信息。
maintainState

falsePageRoute

State

initState

widget

PageStorage

这是一个演示这些概念的示例应用程序。

import 'package:flutter/material.dart';void main() {  runApp(new MaterialApp(    onGenerateRoute: (RouteSettings settings) {      if (settings.name == '/') {        return new MaterialPageRoute<Null>(          settings: settings,          builder: (_) => new MyApp(),          maintainState: false,        );      }      return null;    }  ));}class MyApp extends StatefulWidget {  MyAppState createState() => new MyAppState();}class MyAppState extends State<MyApp> with TickerProviderStateMixin {  AnimationController _controller;  @override  void initState() {    print("initState was called");    _controller = new AnimationController(vsync: this)      ..repeat(min: 0.0, max: 1.0, period: const Duration(seconds: 1))      ..addListener(() {        print('animation value ${_controller.value}');      });    super.initState();  }  @override  void dispose() {    print("dispose was called");    _controller.dispose();    super.dispose();  }  int _counter = 0;  @override  Widget build(BuildContext context) {    return new Scaffold(      appBar: new AppBar(        title: new Text('home screen')      ),      body: new Center(        child: new RaisedButton(          onPressed: () { setState(() {   _counter++; });          },          child: new Text('Button pressed $_counter times'),        ),      ),      floatingActionButton: new FloatingActionButton(        child: new Icon(Icons.remove_red_eye),        onPressed: () {          Navigator.push(context, new MaterialPageRoute( builder: (BuildContext context) {   return new MySecondPage(counter: _counter); },          ));        },      ),    );  }}class MySecondPage extends StatelessWidget {  MySecondPage({ this.counter });  final int counter;  Widget build(BuildContext context) {    return new Scaffold(      appBar: new AppBar(        title: new Text('Certificate of achievement'),      ),      body: new Column(        crossAxisAlignment: CrossAxisAlignment.stretch,        mainAxisAlignment: MainAxisAlignment.spaceAround,        children: [          new Icon(Icons.developer_mode, size: 200.0),          new Text( 'Congrats, you clicked $counter times.', style: Theme.of(context).textTheme.title, textAlign: TextAlign.center,          ),          new Text( 'All your progress has now been lost.', style: Theme.of(context).textTheme.subhead, textAlign: TextAlign.center,          ),        ],      ),    );  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/366768.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号