You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Scaffold中App Bar不显示问题求助

解决Flutter AppBar不显示问题

你的代码逻辑本身没问题,但因为启用了useMaterial3: true,Material3风格下的AppBar默认背景色和Scaffold的背景色高度接近,导致视觉上看起来只有标题文本,没有AppBar的轮廓。

修复方案:

  • 给AppBar显式设置区分度高的背景色
  • 给AppBar添加阴影(elevation)增强层次感

修改后的代码示例:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const HomePage(),
    ),
  );
}

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Register'),
        // 显式设置背景色,和页面背景区分开
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        // 添加阴影增强层次感
        elevation: 4,
      ),
    );
  }
}

另外你提到的flutter clean后出现错误、执行flutter pub get后恢复的操作,只是清理项目缓存并重新拉取依赖,和AppBar不显示的问题无关,不用担忧。

内容的提问来源于stack exchange,提问作者Mohamed Tarek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 04:05:11