• 在子页面加上混合AutomaticKeepAliveClientMixin这个Mixin
  • 重写wantKeepAlive方法,修改返回值为true
  • 在home页面使用IndexedStack来控制页面显示
// home.dart
body: IndexedStack(
          index: 0, //当前的下标
          children: <Widget>[IndexPage(), Buy(), MyPage(), MyPage()]//子页面的Widget
          ),
// buy.dart
import 'package:flutter/material.dart';

class Buy extends StatefulWidget {
  Buy({Key key}) : super(key: key);

  @override
  _BuyState createState() => _BuyState();
}

class _BuyState extends State<Buy> with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin  {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Text("Buy页面"),
    );
  }
}

@override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐