From 3238c3b67f868d464b7a5649a6f5bc2414c605b6 Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 2 Feb 2019 23:57:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20mounted?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/components/disclaimer_msg.dart | 22 ++--- lib/components/list_refresh.dart | 4 +- lib/views/welcome_page/fourth_page.dart | 90 ++++++++++--------- .../components/List/AnimatedList/demo.dart | 8 +- .../Navigation/BottomNavigationBar/demo.dart | 8 +- .../Form/Button/DropdownButton/demo.dart | 8 +- .../Button/FloatingActionButton/index.dart | 8 +- .../Form/Button/IconButton/index.dart | 8 +- .../Form/Button/OutlineButton/index.dart | 8 +- .../Form/Button/RaisedButton/index.dart | 8 +- .../Form/Button/RawMaterialButton/index.dart | 8 +- .../elements/Form/CheckBox/Checkbox/demo.dart | 16 ++-- .../Form/CheckBox/CheckboxListTile/demo.dart | 12 ++- .../elements/Media/Canvas/Canvas/demo.dart | 8 +- .../Canvas/CircleProgressBarPainter/demo.dart | 4 +- .../Media/Canvas/PainterSketch/demo.dart | 36 +++++--- .../Cupertino/CupertinoPicker/demo.dart | 20 +++-- 17 files changed, 164 insertions(+), 112 deletions(-) diff --git a/lib/components/disclaimer_msg.dart b/lib/components/disclaimer_msg.dart index 0f191058..b690c291 100644 --- a/lib/components/disclaimer_msg.dart +++ b/lib/components/disclaimer_msg.dart @@ -33,11 +33,13 @@ class DisclaimerMsgState extends State { void refs(bool value) async { final SharedPreferences prefs = await _prefs; final bool unKnow = value; - setState(() { - _unKnow = prefs.setBool("disclaimer::Boolean", unKnow).then((bool success) { - return unKnow; + if (mounted) { + setState(() { + _unKnow = prefs.setBool("disclaimer::Boolean", unKnow).then((bool success) { + return unKnow; + }); }); - }); + } } @override @@ -135,12 +137,12 @@ class DisclaimerMsgState extends State { tristate: false, value: _valBool, onChanged: (bool bol) { - setState(() { - _valBool = bol; - }); -// refs(bol); - Navigator.of(context) - .pop(); // here I pop to avoid multiple Dialogs + if(mounted) { + setState(() { + _valBool = bol; + }); + } + Navigator.of(context).pop(); // here I pop to avoid multiple Dialogs showAlertDialog(context); //here i call the same function }), Text('不再自动提示', style: TextStyle(fontSize: 14)), diff --git a/lib/components/list_refresh.dart b/lib/components/list_refresh.dart index 0a7dd455..2c999a85 100644 --- a/lib/components/list_refresh.dart +++ b/lib/components/list_refresh.dart @@ -61,7 +61,9 @@ class _ListRefreshState extends State { Future _getMoreData() async { if (!isLoading && _hasMore) { // 如果上一次异步请求数据完成 同时有数据可以加载 - setState(() => isLoading = true); + if (mounted) { + setState(() => isLoading = true); + } //if(_hasMore){ // 还有数据可以拉新 List newEntries = await mokeHttpRequest(); //if (newEntries.isEmpty) { diff --git a/lib/views/welcome_page/fourth_page.dart b/lib/views/welcome_page/fourth_page.dart index 26ec7df1..536885f8 100644 --- a/lib/views/welcome_page/fourth_page.dart +++ b/lib/views/welcome_page/fourth_page.dart @@ -24,52 +24,54 @@ class FourthPageState extends State with TickerProviderStateMixin { slideUpdateStream = new StreamController(); slideUpdateStream.stream.listen((SlideUpdate event) { - setState(() { - if (event.updateType == UpdateType.dragging) { - slideDirection = event.direction; - slidePercent = event.slidePercent; + if(mounted) { + setState(() { + if (event.updateType == UpdateType.dragging) { + slideDirection = event.direction; + slidePercent = event.slidePercent; - if (slideDirection == SlideDirection.leftToRight) { - nextPageIndex = activeIndex - 1; - } else if (slideDirection == SlideDirection.rightToLeft) { - nextPageIndex = activeIndex + 1; - } else { - nextPageIndex = activeIndex; + if (slideDirection == SlideDirection.leftToRight) { + nextPageIndex = activeIndex - 1; + } else if (slideDirection == SlideDirection.rightToLeft) { + nextPageIndex = activeIndex + 1; + } else { + nextPageIndex = activeIndex; + } + } else if (event.updateType == UpdateType.doneDragging) { + if (slidePercent > 0.5) { + animatedPageDragger = new AnimatedPageDragger( + slideDirection: slideDirection, + transitionGoal: TransitionGoal.open, + slidePercent: slidePercent, + slideUpdateStream: slideUpdateStream, + vsync: this, + ); + } else { + animatedPageDragger = new AnimatedPageDragger( + slideDirection: slideDirection, + transitionGoal: TransitionGoal.close, + slidePercent: slidePercent, + slideUpdateStream: slideUpdateStream, + vsync: this, + ); + + nextPageIndex = activeIndex; + } + + animatedPageDragger.run(); + } else if (event.updateType == UpdateType.animating) { + slideDirection = event.direction; + slidePercent = event.slidePercent; + } else if (event.updateType == UpdateType.doneAnimating) { + activeIndex = nextPageIndex; + + slideDirection = SlideDirection.none; + slidePercent = 0.0; + + animatedPageDragger.dispose(); } - } else if (event.updateType == UpdateType.doneDragging) { - if (slidePercent > 0.5) { - animatedPageDragger = new AnimatedPageDragger( - slideDirection: slideDirection, - transitionGoal: TransitionGoal.open, - slidePercent: slidePercent, - slideUpdateStream: slideUpdateStream, - vsync: this, - ); - } else { - animatedPageDragger = new AnimatedPageDragger( - slideDirection: slideDirection, - transitionGoal: TransitionGoal.close, - slidePercent: slidePercent, - slideUpdateStream: slideUpdateStream, - vsync: this, - ); - - nextPageIndex = activeIndex; - } - - animatedPageDragger.run(); - } else if (event.updateType == UpdateType.animating) { - slideDirection = event.direction; - slidePercent = event.slidePercent; - } else if (event.updateType == UpdateType.doneAnimating) { - activeIndex = nextPageIndex; - - slideDirection = SlideDirection.none; - slidePercent = 0.0; - - animatedPageDragger.dispose(); - } - }); + }); + } }); } diff --git a/lib/widgets/components/List/AnimatedList/demo.dart b/lib/widgets/components/List/AnimatedList/demo.dart index f80b6739..43c7de7b 100644 --- a/lib/widgets/components/List/AnimatedList/demo.dart +++ b/lib/widgets/components/List/AnimatedList/demo.dart @@ -43,9 +43,11 @@ class _AnimatedListFullDefault extends State { void remove() { if (_selectedItem != null) { _list.removeAt(_list.indexOf(_selectedItem)); - setState(() { - _selectedItem = null; - }); + if(mounted) { + setState(() { + _selectedItem = null; + }); + } } } diff --git a/lib/widgets/components/Navigation/BottomNavigationBar/demo.dart b/lib/widgets/components/Navigation/BottomNavigationBar/demo.dart index 03c27465..c299017c 100644 --- a/lib/widgets/components/Navigation/BottomNavigationBar/demo.dart +++ b/lib/widgets/components/Navigation/BottomNavigationBar/demo.dart @@ -25,9 +25,11 @@ class _BottomNavigationBarFullDefault extends State { int _currentIndex = 1; void _onItemTapped(int index) { - setState(() { - _currentIndex = index; - }); + if(mounted) { + setState(() { + _currentIndex = index; + }); + } } @override diff --git a/lib/widgets/elements/Form/Button/DropdownButton/demo.dart b/lib/widgets/elements/Form/Button/DropdownButton/demo.dart index 65af9e25..43d90cfd 100644 --- a/lib/widgets/elements/Form/Button/DropdownButton/demo.dart +++ b/lib/widgets/elements/Form/Button/DropdownButton/demo.dart @@ -124,9 +124,11 @@ class DropdownButtonCustom extends StatelessWidget { value: selectItemValue, //下拉菜单item点击之后的回调 onChanged: (T){ - parent.setState((){ - selectItemValue = T; - }); + if (parent.mounted) { + parent.setState(() { + selectItemValue = T; + }); + } }, //设置阴影的高度 elevation: 24, diff --git a/lib/widgets/elements/Form/Button/FloatingActionButton/index.dart b/lib/widgets/elements/Form/Button/FloatingActionButton/index.dart index 9bc53719..13038726 100644 --- a/lib/widgets/elements/Form/Button/FloatingActionButton/index.dart +++ b/lib/widgets/elements/Form/Button/FloatingActionButton/index.dart @@ -50,9 +50,11 @@ class _DemoState extends State { String buttonShapeType = 'border'; // 边框类型 void setButtonShapeType(){ String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border'; - this.setState((){ - buttonShapeType = _buttonShapeType; - }); + if(mounted) { + this.setState(() { + buttonShapeType = _buttonShapeType; + }); + } } @override Widget build(BuildContext context) { diff --git a/lib/widgets/elements/Form/Button/IconButton/index.dart b/lib/widgets/elements/Form/Button/IconButton/index.dart index 94328e59..64f44eaa 100644 --- a/lib/widgets/elements/Form/Button/IconButton/index.dart +++ b/lib/widgets/elements/Form/Button/IconButton/index.dart @@ -45,9 +45,11 @@ class _DemoState extends State { String buttonShapeType = 'border'; // 边框类型 void setButtonShapeType(){ String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border'; - this.setState((){ - buttonShapeType = _buttonShapeType; - }); + if(mounted) { + this.setState(() { + buttonShapeType = _buttonShapeType; + }); + } } @override Widget build(BuildContext context) { diff --git a/lib/widgets/elements/Form/Button/OutlineButton/index.dart b/lib/widgets/elements/Form/Button/OutlineButton/index.dart index c4894e34..e94a4f88 100644 --- a/lib/widgets/elements/Form/Button/OutlineButton/index.dart +++ b/lib/widgets/elements/Form/Button/OutlineButton/index.dart @@ -54,9 +54,11 @@ class _DemoState extends State { String buttonShapeType = 'border'; // 边框类型 void setButtonShapeType(){ String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border'; - this.setState((){ - buttonShapeType = _buttonShapeType; - }); + if (mounted) { + this.setState(() { + buttonShapeType = _buttonShapeType; + }); + } } @override Widget build(BuildContext context) { diff --git a/lib/widgets/elements/Form/Button/RaisedButton/index.dart b/lib/widgets/elements/Form/Button/RaisedButton/index.dart index 9b552503..089ea143 100644 --- a/lib/widgets/elements/Form/Button/RaisedButton/index.dart +++ b/lib/widgets/elements/Form/Button/RaisedButton/index.dart @@ -53,9 +53,11 @@ class _DemoState extends State { String buttonShapeType = 'border'; // 边框类型 void setButtonShapeType(){ String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border'; - this.setState((){ - buttonShapeType = _buttonShapeType; - }); + if(mounted) { + this.setState(() { + buttonShapeType = _buttonShapeType; + }); + } } @override Widget build(BuildContext context) { diff --git a/lib/widgets/elements/Form/Button/RawMaterialButton/index.dart b/lib/widgets/elements/Form/Button/RawMaterialButton/index.dart index 9bced044..581b0c15 100644 --- a/lib/widgets/elements/Form/Button/RawMaterialButton/index.dart +++ b/lib/widgets/elements/Form/Button/RawMaterialButton/index.dart @@ -43,9 +43,11 @@ class _DemoState extends State { String buttonShapeType = 'border'; // 边框类型 void setButtonShapeType(){ //String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border'; - this.setState((){ - //buttonShapeType = _buttonShapeType; - }); + if( mounted ) { + this.setState(() { + //buttonShapeType = _buttonShapeType; + }); + } } @override Widget build(BuildContext context) { diff --git a/lib/widgets/elements/Form/CheckBox/Checkbox/demo.dart b/lib/widgets/elements/Form/CheckBox/Checkbox/demo.dart index c34d10af..a524c0d9 100644 --- a/lib/widgets/elements/Form/CheckBox/Checkbox/demo.dart +++ b/lib/widgets/elements/Form/CheckBox/Checkbox/demo.dart @@ -30,9 +30,11 @@ class _CheckboxDefault extends State { tristate:false, value: isChecked, onChanged: (bool bol) { - setState((){ - isChecked = bol; - }); + if(mounted) { + setState(() { + isChecked = bol; + }); + } } ); } @@ -58,9 +60,11 @@ class CheckboxSelect extends StatelessWidget { tristate:false, value: parent.selectValue == this.index, onChanged: (bool bol) { - parent.setState((){ - parent.selectValue = bol ? this.index : -1; - }); + if(parent.mounted) { + parent.setState(() { + parent.selectValue = bol ? this.index : -1; + }); + } } ); } diff --git a/lib/widgets/elements/Form/CheckBox/CheckboxListTile/demo.dart b/lib/widgets/elements/Form/CheckBox/CheckboxListTile/demo.dart index 2518f5f5..315a607d 100644 --- a/lib/widgets/elements/Form/CheckBox/CheckboxListTile/demo.dart +++ b/lib/widgets/elements/Form/CheckBox/CheckboxListTile/demo.dart @@ -25,7 +25,9 @@ class _CheckboxListTileStateDefault extends State { for (var i = 0; i < isChecks.length; i++) { isChecks[i] = value; } - setState(() => _value = value); + if(mounted) { + setState(() => _value = value); + } } bool isCheck=false; List isChecks=[false,false,false,false]; @@ -55,9 +57,11 @@ class _CheckboxListTileStateDefault extends State { activeColor: _value ? Colors.red : Colors.green, controlAffinity: ListTileControlAffinity.platform, onChanged: (bool){ - setState(() { - isChecks[0]=bool; - }); + if(mounted) { + setState(() { + isChecks[0] = bool; + }); + } }), ), Center( diff --git a/lib/widgets/elements/Media/Canvas/Canvas/demo.dart b/lib/widgets/elements/Media/Canvas/Canvas/demo.dart index 09abd064..a33f58d2 100644 --- a/lib/widgets/elements/Media/Canvas/Canvas/demo.dart +++ b/lib/widgets/elements/Media/Canvas/Canvas/demo.dart @@ -39,9 +39,11 @@ class CustomViewPageState extends State void initState() { super.initState(); getImage("assets/images/painterImg.jpeg").then((data) { - setState(() { - image = data; - }); + if (mounted) { + setState(() { + image = data; + }); + } }); } diff --git a/lib/widgets/elements/Media/Canvas/CircleProgressBarPainter/demo.dart b/lib/widgets/elements/Media/Canvas/CircleProgressBarPainter/demo.dart index ce87e4cc..5ab481b2 100644 --- a/lib/widgets/elements/Media/Canvas/CircleProgressBarPainter/demo.dart +++ b/lib/widgets/elements/Media/Canvas/CircleProgressBarPainter/demo.dart @@ -30,7 +30,9 @@ class CustomViewPageState extends State _doubleAnimation = Tween(begin: 0.0, end: 360.0).animate(_controller); _controller.addListener(() { - this.setState(() {}); + if(mounted){ + this.setState(() {}); + } }); onAnimationStart(); } diff --git a/lib/widgets/elements/Media/Canvas/PainterSketch/demo.dart b/lib/widgets/elements/Media/Canvas/PainterSketch/demo.dart index 52b5f856..c3fbad3b 100644 --- a/lib/widgets/elements/Media/Canvas/PainterSketch/demo.dart +++ b/lib/widgets/elements/Media/Canvas/PainterSketch/demo.dart @@ -30,9 +30,11 @@ class _PainterSketchDomeState extends State { final Offset xy = box.globalToLocal(detail.globalPosition);// 重要需要转换以下坐标位置 Offset p = Offset(xy.dx, xy.dy - 60); //Offset p = Offset(detail.globalPosition.dx, detail.globalPosition.dy - 60); - setState(() { - nowPoints.add(p); - }); + if (mounted) { + setState(() { + nowPoints.add(p); + }); + } } void newGestureDetector(DragStartDetails detail) { @@ -45,9 +47,11 @@ class _PainterSketchDomeState extends State { final Offset xy = box.globalToLocal(detail.globalPosition);// 重要需要转换以下坐标位置 Offset p = Offset(xy.dx, xy.dy - 60); //Offset p = Offset(detail.globalPosition.dx, detail.globalPosition.dy - 60); - setState(() { - nowPoints.add(p); - }); + if (mounted) { + setState(() { + nowPoints.add(p); + }); + } } void changeColor (Color c){ @@ -55,10 +59,12 @@ class _PainterSketchDomeState extends State { LinePoints l = LinePoints(new List.from(nowPoints), nowColor); lines.add(l); } - setState(() { - nowPoints.clear(); - nowColor = c; - }); + if (mounted) { + setState(() { + nowPoints.clear(); + nowColor = c; + }); + } } List colors = [ @@ -74,10 +80,12 @@ class _PainterSketchDomeState extends State { Colors.cyanAccent,]; void _tapClear(){ - setState(() { - lines.clear(); - nowPoints.clear(); - }); + if (mounted) { + setState(() { + lines.clear(); + nowPoints.clear(); + }); + } } @override diff --git a/lib/widgets/themes/Cupertino/CupertinoPicker/demo.dart b/lib/widgets/themes/Cupertino/CupertinoPicker/demo.dart index a45b3cb9..1593f881 100644 --- a/lib/widgets/themes/Cupertino/CupertinoPicker/demo.dart +++ b/lib/widgets/themes/Cupertino/CupertinoPicker/demo.dart @@ -108,7 +108,9 @@ class _CupertinoPickerDemoState extends State { backgroundColor: CupertinoColors.white,// 所有子节点下面的背景颜色 useMagnifier:true,// 是否使用放大效果 onSelectedItemChanged: (int index) { // 当正中间选项改变时的回调 - setState(() => _selectedColorIndex = index); + if (mounted) { + setState(() => _selectedColorIndex = index); + } }, children: List.generate(coolColorNames.length, (int index) { return Center(child: @@ -144,7 +146,9 @@ class _CupertinoPickerDemoState extends State { CupertinoTimerPicker( initialTimerDuration: timer, onTimerDurationChanged: (Duration newTimer) { - setState(() => timer = newTimer); + if (mounted) { + setState(() => timer = newTimer); + } }, ), ); @@ -176,7 +180,9 @@ class _CupertinoPickerDemoState extends State { mode: CupertinoDatePickerMode.date, initialDateTime: date, onDateTimeChanged: (DateTime newDateTime) { - setState(() => date = newDateTime); + if (mounted) { + setState(() => date = newDateTime); + } }, ), ); @@ -206,7 +212,9 @@ class _CupertinoPickerDemoState extends State { mode: CupertinoDatePickerMode.time, initialDateTime: time, onDateTimeChanged: (DateTime newDateTime) { - setState(() => time = newDateTime); + if (mounted) { + setState(() => time = newDateTime); + } }, ), ); @@ -236,7 +244,9 @@ class _CupertinoPickerDemoState extends State { mode: CupertinoDatePickerMode.dateAndTime, initialDateTime: dateTime, onDateTimeChanged: (DateTime newDateTime) { - setState(() => dateTime = newDateTime); + if (mounted) { + setState(() => dateTime = newDateTime); + } }, ), );