Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Result结果): 新增Result结果组件 #220

Merged
merged 5 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tdesign-component/example/assets/api/result_api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## API
### TDResult
#### 默认构造方法

| 参数 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| description | String? | - | 描述文本,用于提供额外信息 |
| icon | Widget? | - | 图标组件,用于在结果中显示一个图标 |
| titleStyle | TextStyle? | - | 自定义字体样式,用于设置标题文本的样式 |
| theme | TDResultTheme | - | 主题样式,定义了结果组件的视觉风格 |
| title | String | - | 标题文本,显示结果的主要信息 |
26 changes: 26 additions & 0 deletions tdesign-component/example/assets/code/result._buildBasicResult.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

Widget _buildBasicResult(BuildContext context) {
return const Column(
children: const [
TDResult(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里多个示例,可以用CodeWrapper拆成多段演示代码,把ExampleItem的ignoreCode设为true,用户会看得更清晰

title: '成功状态',
theme: TDResultTheme.success,
),
SizedBox(height: 48), // 添加间距
TDResult(
title: '失败状态',
theme: TDResultTheme.error,
),
SizedBox(height: 48), // 添加间距
TDResult(
title: '警示状态',
theme: TDResultTheme.warning,
),
SizedBox(height: 48), // 添加间距
TDResult(
title: '默认状态',
theme: TDResultTheme.defaultTheme,
),
],
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Widget _buildCustomResult(BuildContext context) {
return TDResult(
title: '自定义结果',
icon: Image.asset('assets/img/illustration.png'),
description: '描述文字');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

Widget _buildResultWithDescription(BuildContext context) {
return const Column(
children: const [
TDResult(
title: '成功状态',
theme: TDResultTheme.success,
description: '描述文字',
),
SizedBox(height: 48), // 添加间距
TDResult(
title: '失败状态',
theme: TDResultTheme.error,
description: '描述文字',
),
SizedBox(height: 48), // 添加间距
TDResult(
title: '警示状态',
theme: TDResultTheme.warning,
description: '描述文字',
),
SizedBox(height: 48), // 添加间距
TDResult(
title: '默认状态',
theme: TDResultTheme.defaultTheme,
description: '描述文字',
),
],
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tdesign-component/example/lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'page/td_popup_page.dart';
import 'page/td_radio_page.dart';
import 'page/td_radius_page.dart';
import 'page/td_refresh_page.dart';
import 'page/td_result_page.dart';
import 'page/td_search_bar_page.dart';
import 'page/td_shadows_page.dart';
import 'page/td_slider_page.dart';
Expand Down Expand Up @@ -183,8 +184,7 @@ Map<String, List<ExamplePageModel>> exampleMap = {
ExamplePageModel(
text: 'Result 结果',
name: 'result',
isTodo: true,
pageBuilder: _wrapInheritedTheme((context) => const TodoPage())),
pageBuilder: _wrapInheritedTheme((context) => const TDResultPage())),
ExamplePageModel(
text: 'Skeleton 骨架屏',
name: 'skeleton',
Expand Down
174 changes: 174 additions & 0 deletions tdesign-component/example/lib/page/td_result_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '../../base/example_widget.dart';
import '../annotation/demo.dart';

class TDResultPage extends StatefulWidget {
const TDResultPage({Key? key}) : super(key: key);

@override
State<TDResultPage> createState() => _TDResultPageState();
}

class _TDResultPageState extends State<TDResultPage> {
@override
Widget build(BuildContext context) {
return ExamplePage(
title: 'Result 结果',
desc: '用于空状态时的占位提示。',
exampleCodeGroup: 'result',
children: [
ExampleModule(title: '组件类型', children: [
ExampleItem(
desc: '基础结果', ignoreCode: true, builder: _buildBasicResult),
ExampleItem(
desc: '带描述的结果',
ignoreCode: true,
builder: _buildResultWithDescription),
ExampleItem(
desc: '自定义结果', ignoreCode: true, builder: _buildCustomResult),
ExampleItem(
desc: '页面示例', ignoreCode: true, builder: _buildPageExample),
]),
],
);
}

@Demo(group: 'result')
Widget _buildBasicResult(BuildContext context) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个示例方法可以拆成_buildBasicResultSuccess/_buildBasicResultError/_buildBasicResultWarning/_buildBasicResultDefault四个方法,分别在上面打上@demo标签。现在这种拆解,演示代码还是一整块_buildBasicResult方法的内容。
你可以先修改一下,本地无法查看生成演示代码的问题,我后面看下怎么解决吧。

return Column(
children: [
CodeWrapper(
builder: (_) => const TDResult(
title: '成功状态',
theme: TDResultTheme.success,
),
methodName: '_buildBasicResult',
),
const SizedBox(height: 48),
CodeWrapper(
builder: (_) => const TDResult(
title: '失败状态',
theme: TDResultTheme.error,
),
methodName: '_buildBasicResult',
),
const SizedBox(height: 48),
CodeWrapper(
builder: (_) => const TDResult(
title: '警示状态',
theme: TDResultTheme.warning,
),
methodName: '_buildBasicResult',
),
const SizedBox(height: 48),
CodeWrapper(
builder: (_) => const TDResult(
title: '默认状态',
theme: TDResultTheme.defaultTheme,
),
methodName: '_buildBasicResult',
),
],
);
}

@Demo(group: 'result')
Widget _buildResultWithDescription(BuildContext context) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个也是同理,需要拆分一下。最好都拆分成类似_buildCustomResult那样比较单一的实例代码。

return Column(
children: [
CodeWrapper(
builder: (_) => const TDResult(
title: '成功状态',
theme: TDResultTheme.success,
description: '描述文字',
),
methodName: '_buildResultWithDescription',
),
const SizedBox(height: 48),
CodeWrapper(
builder: (_) => const TDResult(
title: '失败状态',
theme: TDResultTheme.error,
description: '描述文字',
),
methodName: '_buildResultWithDescription',
),
const SizedBox(height: 48),
CodeWrapper(
builder: (_) => const TDResult(
title: '警示状态',
theme: TDResultTheme.warning,
description: '描述文字',
),
methodName: '_buildResultWithDescription',
),
const SizedBox(height: 48),
CodeWrapper(
builder: (_) => const TDResult(
title: '默认状态',
theme: TDResultTheme.defaultTheme,
description: '描述文字',
),
methodName: '_buildResultWithDescription',
),
],
);
}

@Demo(group: 'result')
Widget _buildCustomResult(BuildContext context) {
return CodeWrapper(
builder: (_) => TDResult(
title: '自定义结果',
icon: Image.asset('assets/img/illustration.png'),
description: '描述文字',
),
methodName: '_buildCustomResult',
);
}

@Demo(group: 'result')
Widget _buildPageExample(BuildContext context) {
return TDButton(
text: '页面示例',
theme: TDButtonTheme.primary,
size: TDButtonSize.large,
type: TDButtonType.outline,
isBlock: true,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Scaffold(
appBar: AppBar(
title: const Text('Result 结果'),
),
body: Column(
children: [
const SizedBox(height: 48),
const TDResult(
title: '成功状态',
theme: TDResultTheme.success,
description: '描述文字',
),
const SizedBox(height: 48),
TDButton(
text: '返回',
theme: TDButtonTheme.primary,
size: TDButtonSize.large,
type: TDButtonType.outline,
isBlock: true,
onTap: () {
Navigator.pop(context);
},
),
],
),
),
),
);
},
);
}
}
78 changes: 78 additions & 0 deletions tdesign-component/lib/src/components/result/td_result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'package:flutter/material.dart';
import '../../../tdesign_flutter.dart';

enum TDResultTheme { defaultTheme, success, warning, error }

class TDResult extends StatelessWidget {
// 描述文本,用于提供额外信息
final String? description;
// 图标组件,用于在结果中显示一个图标
final Widget? icon;
// 自定义字体样式,用于设置标题文本的样式
final TextStyle? titleStyle;
// 主题样式,定义了结果组件的视觉风格
final TDResultTheme theme;
// 标题文本,显示结果的主要信息
final String title;

// 构造函数,用于初始化Result组件
const TDResult({
Key? key,
this.description,
this.icon,
this.titleStyle,
this.theme = TDResultTheme.defaultTheme, // 默认主题样式为defaultTheme
this.title = '', // 默认标题为空字符串
}) : super(key: key);

@override
Widget build(BuildContext context) {
// 根据主题获取默认的图标组件
Widget displayIcon = icon ?? _getDefaultIconByTheme(context, theme);
// 构建组件布局
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
child: displayIcon,
),
// 如果标题不为空,则显示标题
if (title.isNotEmpty)
Padding(
padding: const EdgeInsets.only(top: 17),
child: TDText(
title,
textColor: TDTheme.of(context).fontGyColor1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里最好能透传一个TextStyle,允许用户自定义文字样式

font: TDTheme.of(context).fontTitleExtraLarge,
style: titleStyle,
)),
// 如果描述不为空,则显示描述
if (description?.isNotEmpty ?? false)
Padding(
padding: const EdgeInsets.only(top: 8),
child: TDText(description!,
textColor: TDTheme.of(context).fontGyColor2,
font: TDTheme.of(context).fontTitleSmall),
),
],
);
}

// 根据主题返回对应的默认图标组件
Widget _getDefaultIconByTheme(BuildContext context, TDResultTheme theme) {
switch (theme) {
case TDResultTheme.success:
return Icon(TDIcons.check_circle,
color: TDTheme.of(context).successNormalColor, size: 70);
case TDResultTheme.warning:
return Icon(TDIcons.error_circle,
color: TDTheme.of(context).warningNormalColor, size: 70);
case TDResultTheme.error:
return Icon(TDIcons.close_circle,
color: TDTheme.of(context).errorNormalColor, size: 70);
default:
return Icon(TDIcons.info_circle,
color: TDTheme.of(context).brandNormalColor, size: 70);
}
}
}
1 change: 1 addition & 0 deletions tdesign-component/lib/tdesign_flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export 'src/components/popup/td_popup_panel.dart';
export 'src/components/popup/td_popup_route.dart';
export 'src/components/radio/td_radio.dart';
export 'src/components/refresh/td_refresh_header.dart';
export 'src/components/result/td_result.dart';
export 'src/components/search/td_search_bar.dart';
export 'src/components/sidebar/td_sidebar.dart';
export 'src/components/sidebar/td_sidebar_controller.dart';
Expand Down