flutter-Scaffold+TabBar+PageView的重绘问题 解决

主布局 继承 SingleTickerProviderStateMixin

import 'package:flutter/material.dart';
import 'package:fl_diletai/utils/tire_export.dart';
import 'print_page.dart';
import 'send_page.dart';
class PrintAndSendGoodPage extends StatefulWidget {
  @override
  _PrintAndSendGoodPageState createState() => _PrintAndSendGoodPageState();
}

class _PrintAndSendGoodPageState extends State<PrintAndSendGoodPage> with SingleTickerProviderStateMixin {
  TabController _tabController;
  final List<Widget> tabBodies = [PrintPage(), SendGoodPage()];
  final List<Tab> tabTitles = [new Tab(text: "打印"), new Tab(text: "发货")];

  // 控制器
  PageController pageController;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(length: tabBodies.length, vsync: this);
    pageController = new PageController();
  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
    pageController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        appBar: CommonAppBar.buildAppBar(
          "打印-发货",
          tabBar: PreferredSize(
            preferredSize: Size.fromHeight(48),
            child: Material(
              color: Colors.white,
              elevation: 1,
              child: TabBar(
                indicatorColor: CommonColors.mainColor,
                indicatorSize: TabBarIndicatorSize.label,
                controller: _tabController,
                indicatorPadding: EdgeInsets.all(8),
                labelColor: CommonColors.mainColor,
                unselectedLabelColor: Colors.black,
                onTap: ((value) {
                  pageController.jumpTo(MediaQuery.of(context).size.width * value);
                }),
                tabs: tabTitles,
              ),
            ),
          ),
        ),
        body: new PageView(
          controller: pageController,
          children: tabBodies,
          onPageChanged: (index) {
            _tabController.animateTo(index);
          },
        ),
      ),
      onWillPop: () {
        return _onWillPop(context);
      },
    );
  }

  Future<bool> _onWillPop(BuildContext context) {
    return showDialog(
          context: context,
          builder: (context) => new AlertDialog(
            title: new Text('退出软件'),
            actions: <Widget>[
              new FlatButton(
                onPressed: () => Navigator.of(context).pop(false),
                child: new Text(
                  '取消',
                  style: TextStyle(color: Colors.black),
                ),
              ),
              new FlatButton(
                onPressed: () async => {
                  Navigator.of(context).pop(true),
                },
                child: new Text(
                  '确定',
                  style: TextStyle(color: Colors.black),
                ),
              ),
            ],
          ),
        ) ??
        false;
  }
}

子布局

继承此方法 AutomaticKeepAliveClientMixin 实现方法 为true 例如
class _PrintPageState extends State<PrintPage> with AutomaticKeepAliveClientMixin {
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值