Flutter 基础组件 Scaffold 详解-3. Scaffold 组件示例

时间:2025-03-13 07:33:23

3.1 appBar(应用顶栏)

  • 类型AppBar

  • 功能:显示页面标题、导航按钮和操作项

  • 代码示例

appBar: AppBar(
  title: Text('首页'),
  actions: [
    IconButton(icon: Icon(Icons.search), onPressed: () {}),
    IconButton(icon: Icon(Icons.settings), onPressed: () {})
  ],
)

3.2 body(主体内容)

  • 类型Widget

  • 功能:承载页面主要内容区域

  • 最佳实践:通常使用 ContainerListView 或 Column 等布局组件包裹内容

3.3 floatingActionButton(悬浮按钮)

  • 类型FloatingActionButton

  • 功能:执行主要操作(如新建、发布等)

  • 定位控制:通过 floatingActionButtonLocation 调整位置

floatingActionButton: FloatingActionButton(
  onPressed: () {},
  child: Icon(Icons.add),
  tooltip: '新建',
)

3.4 drawer(左侧抽屉)

  • 类型Drawer

  • 功能:侧滑显示导航菜单

  • 打开方式:手指从左侧边缘右滑或点击 AppBar 导航图标

drawer: Drawer(
  child: ListView(
    children: [
      ListTile(title: Text('菜单项1'), onTap: () {}),
      ListTile(title: Text('菜单项2'), onTap: () {})
    ],
  ),
)

3.5 bottomNavigationBar(底部导航栏)

  • 类型BottomNavigationBar

  • 功能:实现多页面切换导航

bottomNavigationBar: BottomNavigationBar(
  items: [
    BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
    BottomNavigationBarItem(icon: Icon(Icons.person), label: '我的')
  ],
  currentIndex: _selectedIndex,
  onTap: (index) => setState(() => _selectedIndex = index),
)