47 lines
1.5 KiB
Dart
47 lines
1.5 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||
|
||
/// 详情页
|
||
/// 独立文件 + 公开类 → 别的地方用 import 引入即可
|
||
/// 接收两种参数:构造函数参数(方式1)或命名路由 arguments(方式3)
|
||
class DetailPage extends StatelessWidget {
|
||
const DetailPage({super.key, this.name = '默认', this.age = 0});
|
||
|
||
final String name;
|
||
final int age;
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
// 读取命名路由的 arguments(从 pushNamed 进来时才有)
|
||
final args = ModalRoute.of(context)?.settings.arguments;
|
||
final argMap = args is Map<String, dynamic> ? args : null;
|
||
|
||
return Scaffold(
|
||
appBar: AppBar(title: const Text('详情页')),
|
||
body: Center(
|
||
child: Column(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
// 优先用 arguments 里的值,否则用构造函数传的值
|
||
Text(
|
||
'姓名:${argMap?['name'] ?? name}',
|
||
style: TextStyle(fontSize: 20.sp),
|
||
),
|
||
SizedBox(height: 8.h),
|
||
Text(
|
||
'年龄:${argMap?['age'] ?? age}',
|
||
style: TextStyle(fontSize: 20.sp),
|
||
),
|
||
SizedBox(height: 24.h),
|
||
// 手动 pop(AppBar 的返回箭头也是 pop)
|
||
OutlinedButton(
|
||
onPressed: () => Navigator.pop(context),
|
||
child: const Text('返回上一页'),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|