Hibok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

60 rivejä
1.3 KiB

  1. import 'package:flutter/widgets.dart';
  2. class RestartWidget extends StatefulWidget {
  3. final Widget child;
  4. const RestartWidget({
  5. Key key,
  6. this.child,
  7. }) : super(key: key);
  8. @override
  9. _RestartWidgetState createState() => _RestartWidgetState();
  10. static _RestartWidgetState of(BuildContext context) {
  11. assert(context != null);
  12. return (context
  13. .getElementForInheritedWidgetOfExactType<
  14. _RestartInheritedWidget>()
  15. .widget as _RestartInheritedWidget)
  16. .state;
  17. }
  18. }
  19. class _RestartWidgetState extends State<RestartWidget> {
  20. Key _key = UniqueKey();
  21. /// Change the key to a new one which will make the widget tree
  22. /// re render.
  23. void restartApp() async {
  24. setState(() {
  25. _key = UniqueKey();
  26. });
  27. }
  28. @override
  29. Widget build(BuildContext context) {
  30. return _RestartInheritedWidget(
  31. key: _key,
  32. state: this,
  33. child: widget.child,
  34. );
  35. }
  36. }
  37. class _RestartInheritedWidget extends InheritedWidget {
  38. final _RestartWidgetState state;
  39. _RestartInheritedWidget({
  40. Key key,
  41. this.state,
  42. Widget child,
  43. }) : super(key: key, child: child);
  44. @override
  45. bool updateShouldNotify(InheritedWidget oldWidget) {
  46. return false;
  47. }
  48. }