Skip to content

Commit c214caf

Browse files
committed
InheritedNotifier
1 parent 2ce6fe0 commit c214caf

File tree

7 files changed

+207
-0
lines changed

7 files changed

+207
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import 'package:app/app.dart';
2+
import 'package:flutter/material.dart';
3+
4+
import 'notifier/download_data_scope.dart';
5+
import 'notifier/progress_value_notifier.dart';
6+
import 'page/home/home_page.dart';
7+
8+
void main(){
9+
WidgetsFlutterBinding.ensureInitialized();
10+
runApp(const MyApp());
11+
WindowsAdapter.setSize();
12+
}
13+
14+
class MyApp extends StatelessWidget{
15+
const MyApp({super.key});
16+
17+
@override
18+
Widget build(BuildContext context) {
19+
return DownloadDataScope(
20+
notifier: ProgressValueNotifier(),
21+
child: MaterialApp(
22+
debugShowCheckedModeBanner: false,
23+
theme: ThemeData(
24+
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
25+
useMaterial3: true,
26+
),
27+
home: const ChangeNotifierHome02(),
28+
),
29+
);
30+
}
31+
}
32+
33+
34+
35+
36+
37+
38+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:flutter/cupertino.dart';
2+
3+
import 'progress_value_notifier.dart';
4+
5+
class DownloadDataScope extends InheritedNotifier<ProgressValueNotifier>{
6+
7+
const DownloadDataScope({super.key, required super.child,super.notifier});
8+
9+
static ProgressValueNotifier of(BuildContext context) {
10+
return context.dependOnInheritedWidgetOfExactType<DownloadDataScope>()!.notifier!;
11+
}
12+
13+
static ProgressValueNotifier read(BuildContext context) {
14+
return context.getInheritedWidgetOfExactType<DownloadDataScope>()!.notifier!;
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:flutter/material.dart';
2+
3+
class ProgressValueNotifier with ChangeNotifier{
4+
5+
double _value = 0;
6+
7+
double get value =>_value;
8+
9+
String get valueStr => '${(value*100).toStringAsFixed(1)}%';
10+
11+
set value(double value){
12+
_value = value.clamp(0, 1);
13+
notifyListeners();
14+
}
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
import 'package:flutter/material.dart';
3+
4+
import '../../notifier/download_data_scope.dart';
5+
import '../../notifier/progress_value_notifier.dart';
6+
7+
class DetailProgressView extends StatelessWidget{
8+
const DetailProgressView({super.key});
9+
10+
@override
11+
Widget build(BuildContext context) {
12+
ProgressValueNotifier progress = DownloadDataScope.of(context);
13+
return Stack(
14+
alignment: Alignment.center,
15+
children:[
16+
SizedBox(
17+
width: 200,
18+
height: 50,
19+
child: LinearProgressIndicator(
20+
value: progress.value,
21+
backgroundColor: Colors.grey,
22+
),
23+
),
24+
Text(progress.valueStr,style: TextStyle(color: Colors.white),)
25+
],
26+
);
27+
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:flutter/material.dart';
2+
3+
import 'detail_progress_view.dart';
4+
5+
class DownloadDetailPage extends StatelessWidget {
6+
const DownloadDetailPage({super.key});
7+
8+
@override
9+
Widget build(BuildContext context) {
10+
return Scaffold(
11+
appBar: AppBar(
12+
title: Text('下载详情页'),
13+
),
14+
body: Center(child: DetailProgressView()),
15+
);
16+
}
17+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter/material.dart';
4+
import '../../notifier/download_data_scope.dart';
5+
import '../../notifier/progress_value_notifier.dart';
6+
import '../detail/download_detail.dart';
7+
import 'home_progress_view.dart';
8+
9+
class ChangeNotifierHome02 extends StatefulWidget {
10+
const ChangeNotifierHome02({super.key});
11+
12+
@override
13+
State<ChangeNotifierHome02> createState() => _ChangeNotifierHome02State();
14+
}
15+
16+
class _ChangeNotifierHome02State extends State<ChangeNotifierHome02> {
17+
18+
TextEditingController c = TextEditingController();
19+
@override
20+
Widget build(BuildContext context) {
21+
print("=========_ChangeNotifierHome02State#build===========");
22+
return Scaffold(
23+
floatingActionButton: FloatingActionButton(
24+
onPressed: _startTimer,
25+
child: const Icon(Icons.restart_alt),
26+
),
27+
appBar: AppBar(
28+
title: const Text('下载进度通知模拟'),
29+
actions: [
30+
IconButton(onPressed: (){
31+
Navigator.of(context).push(MaterialPageRoute(builder: (_)=>const DownloadDetailPage()));
32+
}, icon: const Icon(Icons.info_outline))
33+
],
34+
),
35+
body: const Center(child: HomeProgressView()),
36+
);
37+
}
38+
39+
Timer? _timer;
40+
41+
ProgressValueNotifier get progress => DownloadDataScope.read(context);
42+
43+
void _startTimer(){
44+
if(_timer!=null) return;
45+
if(progress.value==1.0){
46+
progress.value=0;
47+
}
48+
_timer = Timer.periodic(const Duration(milliseconds: 200),_updateProgress);
49+
}
50+
51+
52+
void _updateProgress(Timer timer) {
53+
if(progress.value>=1.0){
54+
timer.cancel();
55+
_timer = null;
56+
return;
57+
}
58+
progress.value += 0.01;
59+
}
60+
}
61+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_unit/awesome/listenable/change_notifier_02/notifier/download_data_scope.dart';
4+
5+
import '../../notifier/progress_value_notifier.dart';
6+
7+
class HomeProgressView extends StatelessWidget{
8+
const HomeProgressView({super.key});
9+
10+
11+
@override
12+
Widget build(BuildContext context) {
13+
ProgressValueNotifier progress = DownloadDataScope.of(context);
14+
return Stack(
15+
alignment: Alignment.center,
16+
children:[
17+
SizedBox(
18+
width: 100,
19+
height: 100,
20+
child: CircularProgressIndicator(
21+
value: progress.value,
22+
backgroundColor: Colors.grey,
23+
),
24+
),
25+
Text(progress.valueStr)
26+
],
27+
);
28+
}
29+
30+
31+
32+
}

0 commit comments

Comments
 (0)