- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有一些按钮的Stateful Widget
。在每次按下按钮时,我想显示不同类型的Dialog
。我为Button创建了一个函数,对于每个按钮,我只需调用该函数即可。由于我想在不同的按钮按下时显示不同的Dialog
,因此我尝试将Dialog Widget
传递为按钮函数的参数,并在onPressed
内调用该小部件。当我尝试构建应用程序时,它会显示这些错误。
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following assertion was thrown building DialogPage(dirty, dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#e6799]], state: _DialogPageState#a5060):
setState() or markNeedsBuild() called during build.
This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was: Overlay-[LabeledGlobalKey<OverlayState>#8e4e3]
state: OverlayState#665ea(entries: [OverlayEntry#1cc53(opaque: true; maintainState: false), OverlayEntry#020ad(opaque: false; maintainState: true), OverlayEntry#1d32e(opaque: false; maintainState: false), OverlayEntry#9a4ae(opaque: false; maintainState: true), OverlayEntry#2728f(opaque: false; maintainState: false), OverlayEntry#33d56(opaque: false; maintainState: true)])
The widget which was currently being built when the offending call was made was: DialogPage
dirty
dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#e6799]]
state: _DialogPageState#a5060
The relevant error-causing widget was:
DialogPage file:///C:/Users/imsajib02/AndroidStudioProjects/Flutter/widgets/lib/main.dart:43:32
When the exception was thrown, this was the stack:
#0 Element.markNeedsBuild.<anonymous closure> (package:flutter/src/widgets/framework.dart:3896:11)
#1 Element.markNeedsBuild (package:flutter/src/widgets/framework.dart:3911:6)
#2 State.setState (package:flutter/src/widgets/framework.dart:1168:14)
#3 OverlayState.insertAll (package:flutter/src/widgets/overlay.dart:344:5)
#4 OverlayRoute.install (package:flutter/src/widgets/routes.dart:44:24)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (2) Exception caught by widgets library ═══════════════════════════════════════════════════
The method 'drive' was called on null.
Receiver: null
Tried calling: drive<Color>(Instance of '_ChainedEvaluation<Color>')
The relevant error-causing widget was:
MaterialApp file:///C:/Users/imsajib02/AndroidStudioProjects/Flutter/widgets/lib/main.dart:11:12
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ (3) Exception caught by widgets library ═══════════════════════════════════════════════════
'package:flutter/src/animation/animations.dart': Failed assertion: line 376 pos 15: 'parent != null': is not true.
The relevant error-causing widget was:
MaterialApp file:///C:/Users/imsajib02/AndroidStudioProjects/Flutter/widgets/lib/main.dart:11:12
════════════════════════════════════════════════════════════════════════════════════════════════════
D/FlutterView( 3883): Detaching from a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@e78ea39
class DialogPage extends StatefulWidget {
@override
_DialogPageState createState() => _DialogPageState();
}
class _DialogPageState extends State<DialogPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Dialogs"),
),
body: Center(
child: SingleChildScrollView(
padding: EdgeInsets.only(top: 35),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
verticalDirection: VerticalDirection.down,
children: <Widget>[
Button("DEFAULT DIALOG", DialogCollection().defaultDialog(context)),
Button("DIALOG WITH ICON - 1", DialogCollection().customDialog_1(context)),
Button("Widget", DialogCollection().defaultDialog(context)),
Button("Widget", DialogCollection().defaultDialog(context)),
Button("Widget", DialogCollection().defaultDialog(context)),
Button("Widget", DialogCollection().defaultDialog(context)),
],
),
),
),
);
}
Button(String text, Widget widget) {
return Column(
children: <Widget>[
Container(
width: 230,
child: RaisedButton(
onPressed: () {
},
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(25.0),
//side: BorderSide(color: Colors.red)
),
color: Colors.blueGrey,
padding: EdgeInsets.only(top: 12.0, bottom: 12.0, left: 40.0, right: 40.0),
textColor: Colors.white,
child: Text(text, textAlign: TextAlign.center ,style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
),
),
Container(
height: 35,
)
],
);
}
}
class DialogCollection {
defaultDialog(BuildContext context) {
return showDialog(
context: context,
//barrierDismissible: false, //on touch outside does not dismiss the dialog
builder: (BuildContext context) {
return WillPopScope( //WillPopScope stops dismissing dialog on outside touch and back press
onWillPop: () {
Navigator.of(context).pop();
return null;
},
child: AlertDialog(
title: Text("Dialog Title", textAlign: TextAlign.center,),
titlePadding: const EdgeInsets.all(15.0),
titleTextStyle: TextStyle(
color: Colors.redAccent,
fontSize: 20,
fontWeight: FontWeight.bold,
),
content: Text("This is the description of the dialog."),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
color: Colors.lightBlueAccent,
textColor: Colors.white,
child: Text("Yes", style: TextStyle(
fontSize: 18,
),
),
),
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
color: Colors.redAccent,
textColor: Colors.white,
child: Text("No", style: TextStyle(
fontSize: 18,
),
),
),
],
),
);
}
);
}
}
最佳答案
您可以在下面复制粘贴运行完整代码DialogCollection().defaultDialog(context)
表示执行此功能
您可以使用Button
调用DialogCollection().defaultDialog
来传递函数地址并传递context
程式码片段
Button("DEFAULT DIALOG", DialogCollection().defaultDialog, context)
Button(String text, Function callback, BuildContext context) {
return Column(
children: <Widget>[
Container(
width: 230,
child: RaisedButton(
onPressed: () {
callback(context);
},
import 'package:flutter/material.dart';
class DialogCollection {
defaultDialog(BuildContext context) {
return showDialog(
context: context,
//barrierDismissible: false, //on touch outside does not dismiss the dialog
builder: (BuildContext context) {
return WillPopScope(
//WillPopScope stops dismissing dialog on outside touch and back press
onWillPop: () {
Navigator.of(context).pop();
return null;
},
child: AlertDialog(
title: Text(
"Dialog Title",
textAlign: TextAlign.center,
),
titlePadding: const EdgeInsets.all(15.0),
titleTextStyle: TextStyle(
color: Colors.redAccent,
fontSize: 20,
fontWeight: FontWeight.bold,
),
content: Text("This is the description of the dialog."),
actions: <Widget>[
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
color: Colors.lightBlueAccent,
textColor: Colors.white,
child: Text(
"Yes",
style: TextStyle(
fontSize: 18,
),
),
),
FlatButton(
onPressed: () {
Navigator.of(context).pop();
},
color: Colors.redAccent,
textColor: Colors.white,
child: Text(
"No",
style: TextStyle(
fontSize: 18,
),
),
),
],
),
);
});
}
}
class DialogPage extends StatefulWidget {
@override
_DialogPageState createState() => _DialogPageState();
}
class _DialogPageState extends State<DialogPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Dialogs"),
),
body: Center(
child: SingleChildScrollView(
padding: EdgeInsets.only(top: 35),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
verticalDirection: VerticalDirection.down,
children: <Widget>[
Button(
"DEFAULT DIALOG", DialogCollection().defaultDialog, context),
//Button("DIALOG WITH ICON - 1", DialogCollection().customDialog_1(context)),
/* Button("Widget", DialogCollection().defaultDialog(context)),
Button("Widget", DialogCollection().defaultDialog(context)),
Button("Widget", DialogCollection().defaultDialog(context)),
Button("Widget", DialogCollection().defaultDialog(context)),*/
],
),
),
),
);
}
Button(String text, Function callback, BuildContext context) {
return Column(
children: <Widget>[
Container(
width: 230,
child: RaisedButton(
onPressed: () {
callback(context);
},
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(25.0),
//side: BorderSide(color: Colors.red)
),
color: Colors.blueGrey,
padding: EdgeInsets.only(
top: 12.0, bottom: 12.0, left: 40.0, right: 40.0),
textColor: Colors.white,
child: Text(
text,
textAlign: TextAlign.center,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
),
),
Container(
height: 35,
)
],
);
}
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DialogPage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
关于flutter - 在构建期间在将小部件作为参数传递时调用setstate()或markneedsbuild(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61095142/
我可以在传递给 this.setState() 的回调定义中显式调用 this.setState() 吗? this.setState( { openA:true }, () => {
我有一个从 api 获取数据的场景。在这种情况下,每当我从商店获取新值时,我的 componentWillReceiveProps() 就会被触发。 componentWillReceiveProps
在这个 componentDidUpdate 方法中,在执行 setState 将引号设置为从 fetch 返回的内容后,我必须使用回调再次执行 setState 以将 randomQuoteInde
正如 another question 中向我指出的那样,如果我在 setState、randomQuoteIndex() 中有一个不带参数的函数调用,并且该函数使用该 setState 中设置的状态
问题:当我使用 this.setState 并在回调中输出状态时,它根本不会改变,但是当我将 setstate 嵌套在 setstate 中时,它就会正常工作。 例子:这行不通- this.setSt
这个问题在这里已经有了答案: Why does setState take a closure? (2 个答案) 关闭 4 年前。 我对这两者的区别还是有点迷惑 isBusy = false;
当我使用 setState () 文本出现在调试控制台中.. setState() callback argument returned a Future. The setState() method
这个问题已经有答案了: Are 'Arrow Functions' and 'Functions' equivalent / interchangeable? (4 个回答) React - unca
之间的区别 - this.setState({value: 'xyz', name: 'john', color: 'orange'}) 对比 setValue('xyz'); setName('jo
之间的区别 - this.setState({value: 'xyz', name: 'john', color: 'orange'}) 对比 setValue('xyz'); setName('jo
我在运行代码时收到以下警告: Line 48: Do not mutate state directly. Use setState() react/no-direct-mutation-state
我是 React 的新手,我注意到我们使用 this.setState() 而不是 super.setState() 请我清楚地解释为什么我们用它来调用父类(super class)方法??示例: c
我一生都无法在我的 react 组件中传递这个 TypeError 。我已阅读所有相关主题并实现了几乎所有建议的修复,但均无济于事。 import React, { Component } from
我知道这可能是一个 JavaScript 问题而不是 React 问题,但我无法理解 React setState 的上述签名。 函数参数列表中的方括号和逗号有什么作用? 我知道如何将它与更新程序一起
是否可以在this.setState的回调中调用this.setState? 我正在制作一个 Roguelike Dungeon 并有一个设置,其中在 this.setState 的回调中使用了一个辅
我正在使用 react-navigation 进行路由。在一个组件上,我尝试设置 navigationOptions 以显示汉堡包按钮以打开和关闭侧边栏(抽屉)。所以 onPress 我试图为我的侧边
在之前的 React 版本中,我们可以在状态更改后执行代码,方法如下: setState( prevState => {myval: !prevState.myval}, () => { co
有一个 react 问卷组件,它将选定的答案添加到 this.state = {answer: []} 中,效果很好。同时,当用户更新答案时,它会添加为另一个对象。当questionID相同时,是否有
我正在使用 WebSocket 与我的服务器进行通信,并在我的 handleSubmit() 函数上输入一些值,并在此基础上与服务器进行通信并将我的状态更新为数据从 ws 收到。所以,第一次,一切都工
componentDidMount(prevProps, prevState, prevContext) { let [audioNode, songLen] = [this.refs.aud
我是一名优秀的程序员,十分优秀!