Skip to content

Commit d705f91

Browse files
committed
重启APP
1 parent aa7cccb commit d705f91

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

lib/main.dart

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,55 @@ void main() {
1414
}
1515
}
1616

17-
DragController controller = DragController();
18-
1917
class MyApp extends StatelessWidget {
2018
// This widget is the root of your application.
2119

2220
@override
2321
Widget build(BuildContext context) {
24-
return MaterialApp(
25-
theme: ThemeData(backgroundColor: Colors.white),
26-
home: Scaffold(
27-
resizeToAvoidBottomPadding: false,
28-
body: SplashWidget(),
22+
return RestartWidget(
23+
child: MaterialApp(
24+
theme: ThemeData(backgroundColor: Colors.white),
25+
home: Scaffold(
26+
resizeToAvoidBottomPadding: false,
27+
body: SplashWidget(),
28+
),
2929
),
3030
);
3131
}
3232
}
3333

34+
///这个组件用来重新加载整个child Widget的。当我们需要重启APP的时候,可以使用这个方案
35+
class RestartWidget extends StatefulWidget {
36+
final Widget child;
37+
38+
RestartWidget({Key key, @required this.child})
39+
: assert(child != null),
40+
super(key: key);
41+
42+
static restartApp(BuildContext context) {
43+
final _RestartWidgetState state =
44+
context.ancestorStateOfType(const TypeMatcher<_RestartWidgetState>());
45+
state.restartApp();
46+
}
47+
48+
@override
49+
_RestartWidgetState createState() => _RestartWidgetState();
50+
}
51+
52+
class _RestartWidgetState extends State<RestartWidget> {
53+
Key key = UniqueKey();
54+
55+
void restartApp() {
56+
setState(() {
57+
key = UniqueKey();
58+
});
59+
}
60+
61+
@override
62+
Widget build(BuildContext context) {
63+
return Container(
64+
key: key,
65+
child: widget.child,
66+
);
67+
}
68+
}

lib/pages/person/person_center_page.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:flutter/cupertino.dart';
55
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
66
import 'package:shared_preferences/shared_preferences.dart';
77
import 'package:douban_app/constant/cache_key.dart';
8-
8+
import 'package:douban_app/main.dart';
99
typedef VoidCallback = void Function();
1010

1111
///个人中心
@@ -194,6 +194,7 @@ class _UseNetDataWidgetState extends State<UseNetDataWidget> {
194194
Navigator.of(context).pop();
195195
},),
196196
FlatButton(child: Text('现在重启'),onPressed: (){
197+
RestartWidget.restartApp(context);
197198
Navigator.of(context).pop();
198199
},)
199200
],);

0 commit comments

Comments
 (0)