在Appbar上更新时,Flutter支架更新了整个页面

时间:2019-01-14 01:07:28

标签: flutter

所以我有一个脚手架,是一个列表视图。我有一个管理其阶段的应用程序栏。这是我的应用程序代码:

import 'package:flutter/material.dart';

class HgAppBar extends StatefulWidget implements PreferredSizeWidget {
  final String title;
  final List<Widget> actions;

  HgAppBar({this.title, this.actions, Key key}) : super(key: key);

  @override
  HgAppBarState createState() => HgAppBarState();

  @override
  Size get preferredSize => new Size.fromHeight(kToolbarHeight);
}

class HgAppBarState extends State<HgAppBar> {
  bool _searchOpenned = false;
  void openSeach() {
    setState(() {
      _searchOpenned = true;
    });
  }

  void closeSearch() {
    setState(() {
      _searchOpenned = true;
    });
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: _searchOpenned
          ? TextField(
              decoration: InputDecoration(
                filled: true,
                border: null,
                fillColor: Colors.white,
              ),
              autofocus: true,
            )
          : Text(widget.title ?? 'No title'),
      actions: _searchOpenned
          ? [
              IconButton(
                icon: Icon(Icons.close),
                onPressed: () {
                  setState(() {
                    _searchOpenned = false;
                  });
                },
              )
            ]
          : widget.actions,
    );
  }
}

这是我的页面代码:

class PageSales extends StatefulWidget {
  final Store<AppState> store;
  final String title;
  final bool usePop;
  PageSales(this.store, {this.title, this.usePop = false});
  @override
  State<StatefulWidget> createState() => _PageSales();
}

class _PageSales extends State<PageSales> {
  final appBarKey = GlobalKey<HgAppBarState>();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HgAppBar(
        key: appBarKey,
        title: Localizations.of(context, AppLoc).text('sales_plus'),
        actions: [
          IconButton(
            icon: Icon(Icons.search),
            onPressed: () {
              appBarKey.currentState.openSeach();
            },
          )
        ],
      ),
      body: SafeArea(
        child: Column(children: <Widget>[
          Expanded(
            child: FireStoreListView(
              snapshot: HgFirestore.instance.productCollection.snapshots(),
              itemBuilder: (context, doc) {
                return WidgetProductItem(
                    widget.store, ProductModel.fromDocument(doc));
              },
            ),
          ),
        ]),
      ),
    );
  }
}

所以问题是当我调用openSearch时,我的整个支架都得到刷新(我知道是因为ListView闪烁了)。如何在不刷新整个支架的情况下更新我的应用栏?

1 个答案:

答案 0 :(得分:0)

我试过你的代码,似乎没问题。屏幕没有重建,我使用的是 Flutter 2.2。我建议添加 debugPrint 以确保屏幕重建,ListView 闪烁并不是整个屏幕重建的明确指示。