Active Scaffold 使用方法小结 希望对大家有用

本文介绍在 Rails 应用中如何定制化渲染用户列表的方法,包括修改标题、指定排序方式等技巧。
渲染

# 渲染UsersController
render :active_scaffold => 'users'

# 同样是渲染UsersController,这次将标题改为"活跃用户"
render :active_scaffold => 'users', :label => '活跃用户'

# 按`name`排序,正序(ASC)
render :active_scaffold => 'users', :sort => "name", :sort_direction => "ASC"
`Scaffold` 是 Flutter 中一个非常重要的布局组件,它实现了 Material Design 的基本布局结构。使用 `Scaffold` 可以快速构建一个标准的 Material 风格页面,包含:AppBar、Body、FloatingActionButton、Drawer、BottomNavigationBar 等。 下面是一个完整的使用 `Scaffold` 的示例: ```dart import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Scaffold 示例', theme: ThemeData(primarySwatch: Colors.blue), home: const MyHomePage(), ); } } class MyHomePage extends StatelessWidget { const MyHomePage({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Scaffold 使用示例'), leading: IconButton( icon: const Icon(Icons.menu), onPressed: () { // 打开侧边栏 Scaffold.of(context).openDrawer(); }, ), ), body: const Center( child: Text( '这是页面主体内容', style: TextStyle(fontSize: 18), ), ), drawer: Drawer( child: ListView( padding: EdgeInsets.zero, children: [ const DrawerHeader( decoration: BoxDecoration(color: Colors.blue), child: Text( '菜单标题', style: TextStyle(color: Colors.white, fontSize: 24), ), ), ListTile( leading: const Icon(Icons.home), title: const Text('首页'), onTap: () { Navigator.pop(context); // 关闭抽屉 }, ), ListTile( leading: const Icon(Icons.settings), title: const Text('设置'), onTap: () { Navigator.pop(context); }, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: () { // 显示 SnackBar ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('你点击了 FAB!')), ); }, tooltip: '增加', child: const Icon(Icons.add), ), floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, bottomNavigationBar: BottomAppBar( color: Colors.blue, shape: const CircularNotchedRectangle(), // 与 FAB 形成缺口 child: Container( height: 60, child: Row( mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ IconButton( icon: const Icon(Icons.home, color: Colors.white), onPressed: () {}, ), IconButton( icon: const Icon(Icons.search, color: Colors.white), onPressed: () {}, ), ], ), ), ), ); } } ``` ### 代码解释: - **`Scaffold`**:构建了一个完整的 Material 页面结构。 - **`appBar`**:顶部导航栏,包含标题和左侧菜单按钮。 - **`body`**:页面主要内容区域,居中显示一段文字。 - **`drawer`**:侧滑菜单,通过 `ListView` 构建菜单项,点击可关闭。 - **`floatingActionButton`**:悬浮操作按钮(FAB),点击弹出 `SnackBar`。 - **`floatingActionButtonLocation`**:设置 FAB 的位置为底部居中浮动。 - **`bottomNavigationBar`**:底部导航栏,配合 `CircularNotchedRectangle` 可在中间挖空容纳 FAB。 > 注意:使用 `Scaffold.of(context).openDrawer()` 需要确保 `context` 在 `Scaffold` 内部,否则会报错。也可以用 `Builder` 包裹来获取正确的 context。 --- ### 相关技巧说明: - **`ScaffoldMessenger`**:用于管理 `SnackBar`、`BottomSheet` 等浮层组件,推荐使用它而不是旧的 `Scaffold.of()` 来显示 SnackBar。 - **响应式布局**:可以结合 `MediaQuery` 或 `LayoutBuilder` 实现不同屏幕适配。 - **嵌套 Scaffold**:一般不建议嵌套多个 `Scaffold`,但有时在 Tab 页面中每个 Tab 可以有自己的 `Scaffold`。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值