fix: 部分代码添加 mounted

This commit is contained in:
ryan
2019-02-02 23:57:18 +08:00
parent 57a253e508
commit 3238c3b67f
17 changed files with 164 additions and 112 deletions

View File

@ -33,12 +33,14 @@ class DisclaimerMsgState extends State<DisclaimerMsg> {
void refs(bool value) async { void refs(bool value) async {
final SharedPreferences prefs = await _prefs; final SharedPreferences prefs = await _prefs;
final bool unKnow = value; final bool unKnow = value;
if (mounted) {
setState(() { setState(() {
_unKnow = prefs.setBool("disclaimer::Boolean", unKnow).then((bool success) { _unKnow = prefs.setBool("disclaimer::Boolean", unKnow).then((bool success) {
return unKnow; return unKnow;
}); });
}); });
} }
}
@override @override
void initState() { void initState() {
@ -135,12 +137,12 @@ class DisclaimerMsgState extends State<DisclaimerMsg> {
tristate: false, tristate: false,
value: _valBool, value: _valBool,
onChanged: (bool bol) { onChanged: (bool bol) {
if(mounted) {
setState(() { setState(() {
_valBool = bol; _valBool = bol;
}); });
// refs(bol); }
Navigator.of(context) Navigator.of(context).pop(); // here I pop to avoid multiple Dialogs
.pop(); // here I pop to avoid multiple Dialogs
showAlertDialog(context); //here i call the same function showAlertDialog(context); //here i call the same function
}), }),
Text('不再自动提示', style: TextStyle(fontSize: 14)), Text('不再自动提示', style: TextStyle(fontSize: 14)),

View File

@ -61,7 +61,9 @@ class _ListRefreshState extends State<ListRefresh> {
Future _getMoreData() async { Future _getMoreData() async {
if (!isLoading && _hasMore) { if (!isLoading && _hasMore) {
// 如果上一次异步请求数据完成 同时有数据可以加载 // 如果上一次异步请求数据完成 同时有数据可以加载
if (mounted) {
setState(() => isLoading = true); setState(() => isLoading = true);
}
//if(_hasMore){ // 还有数据可以拉新 //if(_hasMore){ // 还有数据可以拉新
List newEntries = await mokeHttpRequest(); List newEntries = await mokeHttpRequest();
//if (newEntries.isEmpty) { //if (newEntries.isEmpty) {

View File

@ -24,6 +24,7 @@ class FourthPageState extends State<FourthPage> with TickerProviderStateMixin {
slideUpdateStream = new StreamController<SlideUpdate>(); slideUpdateStream = new StreamController<SlideUpdate>();
slideUpdateStream.stream.listen((SlideUpdate event) { slideUpdateStream.stream.listen((SlideUpdate event) {
if(mounted) {
setState(() { setState(() {
if (event.updateType == UpdateType.dragging) { if (event.updateType == UpdateType.dragging) {
slideDirection = event.direction; slideDirection = event.direction;
@ -70,6 +71,7 @@ class FourthPageState extends State<FourthPage> with TickerProviderStateMixin {
animatedPageDragger.dispose(); animatedPageDragger.dispose();
} }
}); });
}
}); });
} }

View File

@ -43,11 +43,13 @@ class _AnimatedListFullDefault extends State<AnimatedListFullDefault> {
void remove() { void remove() {
if (_selectedItem != null) { if (_selectedItem != null) {
_list.removeAt(_list.indexOf(_selectedItem)); _list.removeAt(_list.indexOf(_selectedItem));
if(mounted) {
setState(() { setState(() {
_selectedItem = null; _selectedItem = null;
}); });
} }
} }
}
Widget _buildItem(BuildContext context, int index, Animation<double> animation) { Widget _buildItem(BuildContext context, int index, Animation<double> animation) {
return CardItem( return CardItem(

View File

@ -25,10 +25,12 @@ class _BottomNavigationBarFullDefault extends State {
int _currentIndex = 1; int _currentIndex = 1;
void _onItemTapped(int index) { void _onItemTapped(int index) {
if(mounted) {
setState(() { setState(() {
_currentIndex = index; _currentIndex = index;
}); });
} }
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {

View File

@ -124,9 +124,11 @@ class DropdownButtonCustom extends StatelessWidget {
value: selectItemValue, value: selectItemValue,
//下拉菜单item点击之后的回调 //下拉菜单item点击之后的回调
onChanged: (T){ onChanged: (T){
parent.setState((){ if (parent.mounted) {
parent.setState(() {
selectItemValue = T; selectItemValue = T;
}); });
}
}, },
//设置阴影的高度 //设置阴影的高度
elevation: 24, elevation: 24,

View File

@ -50,10 +50,12 @@ class _DemoState extends State<Demo> {
String buttonShapeType = 'border'; // 边框类型 String buttonShapeType = 'border'; // 边框类型
void setButtonShapeType(){ void setButtonShapeType(){
String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border'; String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border';
this.setState((){ if(mounted) {
this.setState(() {
buttonShapeType = _buttonShapeType; buttonShapeType = _buttonShapeType;
}); });
} }
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return WidgetDemo( return WidgetDemo(

View File

@ -45,10 +45,12 @@ class _DemoState extends State<Demo> {
String buttonShapeType = 'border'; // 边框类型 String buttonShapeType = 'border'; // 边框类型
void setButtonShapeType(){ void setButtonShapeType(){
String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border'; String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border';
this.setState((){ if(mounted) {
this.setState(() {
buttonShapeType = _buttonShapeType; buttonShapeType = _buttonShapeType;
}); });
} }
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return WidgetDemo( return WidgetDemo(

View File

@ -54,10 +54,12 @@ class _DemoState extends State<Demo> {
String buttonShapeType = 'border'; // 边框类型 String buttonShapeType = 'border'; // 边框类型
void setButtonShapeType(){ void setButtonShapeType(){
String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border'; String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border';
this.setState((){ if (mounted) {
this.setState(() {
buttonShapeType = _buttonShapeType; buttonShapeType = _buttonShapeType;
}); });
} }
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return WidgetDemo( return WidgetDemo(

View File

@ -53,10 +53,12 @@ class _DemoState extends State<Demo> {
String buttonShapeType = 'border'; // 边框类型 String buttonShapeType = 'border'; // 边框类型
void setButtonShapeType(){ void setButtonShapeType(){
String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border'; String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border';
this.setState((){ if(mounted) {
this.setState(() {
buttonShapeType = _buttonShapeType; buttonShapeType = _buttonShapeType;
}); });
} }
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return WidgetDemo( return WidgetDemo(

View File

@ -43,10 +43,12 @@ class _DemoState extends State<Demo> {
String buttonShapeType = 'border'; // 边框类型 String buttonShapeType = 'border'; // 边框类型
void setButtonShapeType(){ void setButtonShapeType(){
//String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border'; //String _buttonShapeType = (buttonShapeType == 'border') ? 'radius' : 'border';
this.setState((){ if( mounted ) {
this.setState(() {
//buttonShapeType = _buttonShapeType; //buttonShapeType = _buttonShapeType;
}); });
} }
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return WidgetDemo( return WidgetDemo(

View File

@ -30,10 +30,12 @@ class _CheckboxDefault extends State {
tristate:false, tristate:false,
value: isChecked, value: isChecked,
onChanged: (bool bol) { onChanged: (bool bol) {
setState((){ if(mounted) {
setState(() {
isChecked = bol; isChecked = bol;
}); });
} }
}
); );
} }
} }
@ -58,10 +60,12 @@ class CheckboxSelect extends StatelessWidget {
tristate:false, tristate:false,
value: parent.selectValue == this.index, value: parent.selectValue == this.index,
onChanged: (bool bol) { onChanged: (bool bol) {
parent.setState((){ if(parent.mounted) {
parent.setState(() {
parent.selectValue = bol ? this.index : -1; parent.selectValue = bol ? this.index : -1;
}); });
} }
}
); );
} }
} }

View File

@ -25,8 +25,10 @@ class _CheckboxListTileStateDefault extends State {
for (var i = 0; i < isChecks.length; i++) { for (var i = 0; i < isChecks.length; i++) {
isChecks[i] = value; isChecks[i] = value;
} }
if(mounted) {
setState(() => _value = value); setState(() => _value = value);
} }
}
bool isCheck=false; bool isCheck=false;
List<bool> isChecks=[false,false,false,false]; List<bool> isChecks=[false,false,false,false];
@override @override
@ -55,9 +57,11 @@ class _CheckboxListTileStateDefault extends State {
activeColor: _value ? Colors.red : Colors.green, activeColor: _value ? Colors.red : Colors.green,
controlAffinity: ListTileControlAffinity.platform, controlAffinity: ListTileControlAffinity.platform,
onChanged: (bool){ onChanged: (bool){
if(mounted) {
setState(() { setState(() {
isChecks[0]=bool; isChecks[0] = bool;
}); });
}
}), }),
), ),
Center( Center(

View File

@ -39,9 +39,11 @@ class CustomViewPageState extends State<CustomViewPage>
void initState() { void initState() {
super.initState(); super.initState();
getImage("assets/images/painterImg.jpeg").then((data) { getImage("assets/images/painterImg.jpeg").then((data) {
if (mounted) {
setState(() { setState(() {
image = data; image = data;
}); });
}
}); });
} }

View File

@ -30,7 +30,9 @@ class CustomViewPageState extends State<CustomViewPage>
_doubleAnimation = Tween(begin: 0.0, end: 360.0).animate(_controller); _doubleAnimation = Tween(begin: 0.0, end: 360.0).animate(_controller);
_controller.addListener(() { _controller.addListener(() {
if(mounted){
this.setState(() {}); this.setState(() {});
}
}); });
onAnimationStart(); onAnimationStart();
} }

View File

@ -30,10 +30,12 @@ class _PainterSketchDomeState extends State<PainterSketchDome> {
final Offset xy = box.globalToLocal(detail.globalPosition);// 重要需要转换以下坐标位置 final Offset xy = box.globalToLocal(detail.globalPosition);// 重要需要转换以下坐标位置
Offset p = Offset(xy.dx, xy.dy - 60); Offset p = Offset(xy.dx, xy.dy - 60);
//Offset p = Offset(detail.globalPosition.dx, detail.globalPosition.dy - 60); //Offset p = Offset(detail.globalPosition.dx, detail.globalPosition.dy - 60);
if (mounted) {
setState(() { setState(() {
nowPoints.add(p); nowPoints.add(p);
}); });
} }
}
void newGestureDetector(DragStartDetails detail) { void newGestureDetector(DragStartDetails detail) {
if (nowPoints.length != 0) { if (nowPoints.length != 0) {
@ -45,21 +47,25 @@ class _PainterSketchDomeState extends State<PainterSketchDome> {
final Offset xy = box.globalToLocal(detail.globalPosition);// 重要需要转换以下坐标位置 final Offset xy = box.globalToLocal(detail.globalPosition);// 重要需要转换以下坐标位置
Offset p = Offset(xy.dx, xy.dy - 60); Offset p = Offset(xy.dx, xy.dy - 60);
//Offset p = Offset(detail.globalPosition.dx, detail.globalPosition.dy - 60); //Offset p = Offset(detail.globalPosition.dx, detail.globalPosition.dy - 60);
if (mounted) {
setState(() { setState(() {
nowPoints.add(p); nowPoints.add(p);
}); });
} }
}
void changeColor (Color c){ void changeColor (Color c){
if (nowPoints.length != 0) { if (nowPoints.length != 0) {
LinePoints l = LinePoints(new List<Offset>.from(nowPoints), nowColor); LinePoints l = LinePoints(new List<Offset>.from(nowPoints), nowColor);
lines.add(l); lines.add(l);
} }
if (mounted) {
setState(() { setState(() {
nowPoints.clear(); nowPoints.clear();
nowColor = c; nowColor = c;
}); });
} }
}
List<Color> colors = <Color>[ List<Color> colors = <Color>[
Colors.redAccent, Colors.redAccent,
@ -74,11 +80,13 @@ class _PainterSketchDomeState extends State<PainterSketchDome> {
Colors.cyanAccent,]; Colors.cyanAccent,];
void _tapClear(){ void _tapClear(){
if (mounted) {
setState(() { setState(() {
lines.clear(); lines.clear();
nowPoints.clear(); nowPoints.clear();
}); });
} }
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {

View File

@ -108,7 +108,9 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
backgroundColor: CupertinoColors.white,// 所有子节点下面的背景颜色 backgroundColor: CupertinoColors.white,// 所有子节点下面的背景颜色
useMagnifier:true,// 是否使用放大效果 useMagnifier:true,// 是否使用放大效果
onSelectedItemChanged: (int index) { // 当正中间选项改变时的回调 onSelectedItemChanged: (int index) { // 当正中间选项改变时的回调
if (mounted) {
setState(() => _selectedColorIndex = index); setState(() => _selectedColorIndex = index);
}
}, },
children: List<Widget>.generate(coolColorNames.length, (int index) { children: List<Widget>.generate(coolColorNames.length, (int index) {
return Center(child: return Center(child:
@ -144,7 +146,9 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
CupertinoTimerPicker( CupertinoTimerPicker(
initialTimerDuration: timer, initialTimerDuration: timer,
onTimerDurationChanged: (Duration newTimer) { onTimerDurationChanged: (Duration newTimer) {
if (mounted) {
setState(() => timer = newTimer); setState(() => timer = newTimer);
}
}, },
), ),
); );
@ -176,7 +180,9 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
mode: CupertinoDatePickerMode.date, mode: CupertinoDatePickerMode.date,
initialDateTime: date, initialDateTime: date,
onDateTimeChanged: (DateTime newDateTime) { onDateTimeChanged: (DateTime newDateTime) {
if (mounted) {
setState(() => date = newDateTime); setState(() => date = newDateTime);
}
}, },
), ),
); );
@ -206,7 +212,9 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
mode: CupertinoDatePickerMode.time, mode: CupertinoDatePickerMode.time,
initialDateTime: time, initialDateTime: time,
onDateTimeChanged: (DateTime newDateTime) { onDateTimeChanged: (DateTime newDateTime) {
if (mounted) {
setState(() => time = newDateTime); setState(() => time = newDateTime);
}
}, },
), ),
); );
@ -236,7 +244,9 @@ class _CupertinoPickerDemoState extends State<CupertinoPickerDemo> {
mode: CupertinoDatePickerMode.dateAndTime, mode: CupertinoDatePickerMode.dateAndTime,
initialDateTime: dateTime, initialDateTime: dateTime,
onDateTimeChanged: (DateTime newDateTime) { onDateTimeChanged: (DateTime newDateTime) {
if (mounted) {
setState(() => dateTime = newDateTime); setState(() => dateTime = newDateTime);
}
}, },
), ),
); );