11import 'package:flutter/material.dart' ;
2- import 'package:tdesign_flutter/tdesign_flutter.dart' ;
3- import './progress.dart' ;
2+ // 假设Progress组件在一个单独的文件中
3+ import 'td_progress.dart' ;
4+ import 'dart:async' ;
45
56void main () {
67 runApp (const MyApp ());
@@ -11,9 +12,12 @@ class MyApp extends StatelessWidget {
1112
1213 @override
1314 Widget build (BuildContext context) {
14- return const MaterialApp (
15- title: 'Flutter Demo' ,
16- home: Home (title: 'Flutter Demo Home Page' ),
15+ return MaterialApp (
16+ title: '进度条演示' ,
17+ theme: ThemeData (
18+ primarySwatch: Colors .blue,
19+ ),
20+ home: const Home (title: '进度条演示主页' ),
1721 );
1822 }
1923}
@@ -28,66 +32,172 @@ class Home extends StatefulWidget {
2832}
2933
3034class _HomeState extends State <Home > {
31- TextLabel buttonLabel = const TextLabel ("50%" );
35+ TDLabelWidget buttonLabel = const TDTextLabel ("0%" );
36+ double progressValue = 0.0 ;
37+ Timer ? _timer;
38+ bool isProgressing = false ;
39+
40+ // 新增:用于微型按钮进度条的状态
41+ bool isPlaying = false ;
42+ double microProgressValue = 0.0 ;
43+ Timer ? _microTimer;
44+
45+ @override
46+ void dispose () {
47+ _timer? .cancel ();
48+ _microTimer? .cancel ();
49+ super .dispose ();
50+ }
51+
52+ void _toggleProgress () {
53+ if (isProgressing) {
54+ // 暂停进度
55+ _timer? .cancel ();
56+ setState (() {
57+ buttonLabel = const TDTextLabel ("继续" );
58+ isProgressing = false ;
59+ });
60+ } else {
61+ // 开始或继续进度
62+ _timer? .cancel ();
63+ _timer = Timer .periodic (const Duration (milliseconds: 100 ), (timer) {
64+ setState (() {
65+ if (progressValue < 1.0 ) {
66+ progressValue += 0.01 ;
67+ buttonLabel = TDTextLabel ("${(progressValue * 100 ).toInt ()}%" );
68+ } else {
69+ _timer? .cancel ();
70+ buttonLabel = const TDTextLabel ("完成" );
71+ isProgressing = false ;
72+ }
73+ });
74+ });
75+ setState (() {
76+ isProgressing = true ;
77+ });
78+ }
79+ }
80+
81+ void _resetProgress () {
82+ _timer? .cancel ();
83+ setState (() {
84+ progressValue = 0.0 ;
85+ buttonLabel = const TDTextLabel ("开始" );
86+ isProgressing = false ;
87+ });
88+ }
89+ // 新增:控制微型按钮进度条的方法
90+ void _toggleMicroProgress () {
91+ setState (() {
92+ isPlaying = ! isPlaying;
93+ });
94+ if (isPlaying) {
95+ _microTimer = Timer .periodic (const Duration (milliseconds: 100 ), (timer) {
96+ setState (() {
97+ if (microProgressValue < 1.0 ) {
98+ microProgressValue += 0.01 ;
99+ } else {
100+ _microTimer? .cancel ();
101+ isPlaying = false ;
102+ microProgressValue = 0.0 ;
103+ }
104+ });
105+ });
106+ } else {
107+ _microTimer? .cancel ();
108+ }
109+ }
110+
111+ void _resetMicroProgress () {
112+ _microTimer? .cancel ();
113+ setState (() {
114+ isPlaying = false ;
115+ microProgressValue = 0.0 ;
116+ });
117+ }
118+
32119 @override
33120 Widget build (BuildContext context) {
34121 return Scaffold (
35122 appBar: AppBar (title: const Text ('自定义百分比进度条' )),
36123 body: ListView (
37124 padding: const EdgeInsets .all (16.0 ),
38125 children: [
39- _buildSection ("线形进度条" , [
40- _buildProgressItem ("无确定值" , Progress .linear (strokeWidth: 5 )),
41- _buildProgressItem ("有确定值,小于等于10%" , Progress .linear (value: 0.1 )),
42- _buildProgressItem ("有确定值,大于10%" , Progress .linear (value: 0.5 )),
43- _buildProgressItem ("label靠左,大于10%" , Progress .linear (
44- value: 0.5 , progressLabelPosition: ProgressLabelPosition .left)),
45- _buildProgressItem ("自定义label" , Progress .linear (value: 0.5 , label: TextLabel ("百分之50" ))),
46- _buildProgressItem ("label靠右,大于10%" , Progress .linear (
47- value: 0.5 , progressLabelPosition: ProgressLabelPosition .right)),
48- _buildProgressItem ("更改status" , Progress .linear (value: 0.5 , progressStatus: ProgressStatus .warning)),
49- _buildProgressItem ("更改status,且Label在外" , Progress .linear (
126+ _buildSection ("线性进度条" , [
127+ _buildProgressItem ("不确定" , const TDProgress (type: TDProgressType .linear, strokeWidth: 5 )),
128+ _buildProgressItem ("确定, ≤ 10%" , const TDProgress (type: TDProgressType .linear, value: 0.1 )),
129+ _buildProgressItem ("确定, > 10%" , const TDProgress (type: TDProgressType .linear, value: 0.5 )),
130+ _buildProgressItem ("左侧标签, > 10%" , const TDProgress (
131+ type: TDProgressType .linear, value: 0.5 , progressLabelPosition: TDProgressLabelPosition .left)),
132+ _buildProgressItem ("自定义标签" , const TDProgress (type: TDProgressType .linear, value: 0.5 , label: TDTextLabel ("百分之50" ))),
133+ _buildProgressItem ("右侧标签, > 10%" , const TDProgress (
134+ type: TDProgressType .linear, value: 0.5 , progressLabelPosition: TDProgressLabelPosition .right)),
135+ _buildProgressItem ("警告状态" , const TDProgress (type: TDProgressType .linear, value: 0.5 , progressStatus: TDProgressStatus .warning)),
136+ _buildProgressItem ("危险状态, 外部标签" , const TDProgress (
137+ type: TDProgressType .linear,
50138 value: 0.5 ,
51- progressStatus: ProgressStatus .danger,
52- progressLabelPosition: ProgressLabelPosition .right)),
139+ progressStatus: TDProgressStatus .danger,
140+ progressLabelPosition: TDProgressLabelPosition .right)),
53141 ]),
54- _buildSection ("环形进度条 " , [
55- Row (
142+ _buildSection ("圆形进度条 " , [
143+ const Row (
56144 mainAxisAlignment: MainAxisAlignment .spaceEvenly,
57145 children: [
58- Progress .circular ( ),
59- Progress .circular ( value: 0.5 ),
60- Progress . circular (
61- value: 0.5 , progressStatus: ProgressStatus .success),
62- Progress . circular (
63- value: 0.7 , progressStatus: ProgressStatus .warning),
146+ TDProgress (type : TDProgressType .circular),
147+ TDProgress (type : TDProgressType .circular, value: 0.5 ),
148+ TDProgress (
149+ type : TDProgressType .circular, value: 0.5 , progressStatus: TDProgressStatus .success),
150+ TDProgress (
151+ type : TDProgressType .circular, value: 0.7 , progressStatus: TDProgressStatus .warning),
64152 ],
65153 ),
66154 ]),
67155 _buildSection ("微型进度条" , [
68- Progress .micro ( value: 0.3 ),
156+ const TDProgress (type : TDProgressType .micro, value: 0.3 ),
69157 ]),
70158 _buildSection ("微型按钮进度条" , [
71159 Row (
160+ children: [
161+ TDProgress (type: TDProgressType .micro, value: 0.3 , onTap: (){}, label: const TDIconLabel (Icons .play_arrow, color: Colors .blue)),
162+ const SizedBox (width: 10 ),
163+ TDProgress (type: TDProgressType .micro, value: 0.3 , onTap: (){}, label: const TDIconLabel (Icons .pause, color: Colors .blue)),
164+ const SizedBox (width: 10 ),
165+ TDProgress (type: TDProgressType .micro, value: 0.3 , onTap: (){}, label: const TDIconLabel (Icons .stop, color: Colors .blue))
166+ ]
167+ )
168+ ]),
169+ _buildSection ("交互式微型按钮进度条" , [
170+ Row (
171+ mainAxisAlignment: MainAxisAlignment .start,
72172 children: [
73- Progress .micro (value: 0.3 , onTap: (){}, label: const IconLabel (Icons .play_arrow, color: Colors .blue)),
74- const SizedBox (width: 10 ),
75- Progress .micro (value: 0.3 , onTap: (){}, label: const IconLabel (Icons .pause, color: Colors .blue)),
173+ TDProgress (
174+ type: TDProgressType .micro,
175+ value: microProgressValue,
176+ onTap: _toggleMicroProgress,
177+ label: TDIconLabel (isPlaying ? Icons .pause : Icons .play_arrow, color: Colors .blue),
178+ ),
76179 const SizedBox (width: 10 ),
77- Progress .micro (value: 0.3 , onTap: (){}, label: const IconLabel (Icons .stop, color: Colors .blue))
78- ]
79- )
180+ TDProgress (
181+ type: TDProgressType .micro,
182+ value: microProgressValue,
183+ onTap: _resetMicroProgress,
184+ label: const TDIconLabel (Icons .stop, color: Colors .blue),
185+ ),
186+ ],
187+ ),
188+ const SizedBox (height: 10 ),
189+ Text ('点击播放/暂停,点击停止重置进度' , style: Theme .of (context).textTheme.bodyMedium),
80190 ]),
81191 _buildSection ("按钮进度条" , [
82- Progress .button (
83- onTap: (){
84- setState (() {
85- buttonLabel = buttonLabel.data == "50%" ? const TextLabel ("继续" ) : const TextLabel ("50%" );
86- });
87- },
88- value: .5 ,
192+ TDProgress (
193+ type: TDProgressType .button,
194+ onTap: _toggleProgress,
195+ onLongPress: _resetProgress,
196+ value: progressValue,
89197 label: buttonLabel,
90198 ),
199+ const SizedBox (height: 10 ),
200+ Text ('点击开始/暂停进度,长按重置进度' , style: Theme .of (context).textTheme.bodyMedium),
91201 ])
92202 ],
93203 ),
0 commit comments