【问题标题】:Flutter scaffold updated entire page when update on appbar在 appbar 上更新时,Flutter 脚手架更新了整个页面
【发布时间】:2019-06-08 00:56:54
【问题描述】:

所以我有一个带有 body 的脚手架是一个列表视图。我有一个管理其阶段的应用栏。这是我的应用条码:

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 正在闪烁)。如何在不刷新整个脚手架的情况下更新我的 appbar?

【问题讨论】:

  • 可以在添加脚手架的时候添加代码吗?
  • 您确定您的列表视图已重建吗?添加打印(“建筑...”);内部构建方法并检查它是否打印
  • 列表正在闪烁,所以我确定它正在重建/重绘

标签: flutter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2011-12-08
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    相关资源
    最近更新 更多