码迷,mamicode.com
首页 > 其他好文 > 详细

flutter 保持页面状态

时间:2019-08-23 22:46:07      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:定义   自己   creat   code   child   dir   body   ali   pos   

import ‘package:flutter/material.dart‘;

import ‘KeepAliveDemo.dart‘;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: ‘Flutter Demo‘,
      theme: ThemeData(
        primarySwatch: Colors.green,  //定义主题风格    primarySwatch
      ),
      home: KeepAliveDemo(),
    );
  }

}
import ‘package:flutter/material.dart‘;
import ‘package:flutter_app/MyHomePage.dart‘;
class KeepAliveDemo extends StatefulWidget {
  _KeepAliveDemoState createState() => _KeepAliveDemoState();
}
/*
with是dart的关键字,意思是混入的意思,
就是说可以将一个或者多个类的功能添加到自己的类无需继承这些类,
避免多重继承导致的问题。
SingleTickerProviderStateMixin 主要是我们初始化TabController时,
需要用到vsync ,垂直属性,然后传递this
*/
class _KeepAliveDemoState extends State<KeepAliveDemo> with SingleTickerProviderStateMixin {
  TabController _controller;

  @override
  void initState(){
    super.initState();
    _controller = TabController(length:3, vsync: this);
  }

  //重写被释放的方法,只释放TabController
  @override
  void dispose(){
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar:AppBar(
            title:Text(‘Keep Alive Demo‘),
            bottom:TabBar(
              controller: _controller,
              tabs:[
                Tab(icon:Icon(Icons.directions_car)),
                Tab(icon:Icon(Icons.directions_transit)),
                Tab(icon:Icon(Icons.directions_bike)),
              ],
            )
        ),
        body:TabBarView(
          controller: _controller,
          children: <Widget>[
            MyHomePage(),
            MyHomePage(),
            MyHomePage()
          ],
        )
    );
  }
}

效果:

技术图片

flutter 保持页面状态

标签:定义   自己   creat   code   child   dir   body   ali   pos   

原文地址:https://www.cnblogs.com/loaderman/p/11350208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!