-
Notifications
You must be signed in to change notification settings - Fork 97
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | - | 标题文本,显示结果的主要信息 | |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
Widget _buildBasicResult(BuildContext context) { | ||
return const Column( | ||
children: const [ | ||
TDResult( | ||
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: '描述文字', | ||
), | ||
], | ||
); | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}, | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
}, | ||
); | ||
} | ||
} |
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里多个示例,可以用CodeWrapper拆成多段演示代码,把ExampleItem的ignoreCode设为true,用户会看得更清晰