我创建了一个PostUpdaterWidget扩展StatelessWidget,TextEditingControllers用于测试 Bloc 模式的实现。
final _usernameController = TextEditingController();
final _contentController = TextEditingController();
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
TextField(
controller: _usernameController,
decoration: InputDecoration(hintText: "Post Username"),
),
TextField(
controller: _contentController,
decoration: InputDecoration(hintText: "Post Content"),
),
Container(
height: 16,
),
RaisedButton(
child: Text("Update Post"),
onPressed: () => _updatePost(context),
)
],
);
}
_updatePost(BuildContext context) {
print("Processing Post Update");
String username = _usernameController.text.trim();
String content = _contentController.text.trim();
Post post = new Post();
post.id …Run Code Online (Sandbox Code Playgroud) 如何在 PageView onPageChanged 中获取上一个索引?目前,当页面更改时,我可以使用以下代码获取当前索引:
PageView(
children: <Widget>[
SomeView(),
SomeOtherViews(),
SomeOtherViews(),
],
controller: _pageViewController,
onPageChanged: _onPageViewChange,
);
_onPageViewChange(int page) {
print("Current Page: " + page.toString());
}
Run Code Online (Sandbox Code Playgroud)
颤振是否有任何内置功能?或者我应该手动保存页面作为上一页的参考?
我对颤振很陌生,我正在尝试学习如何创建视图。我试图创建一个单独的视图文件或小部件,如果它是在颤振中调用的,并且只需从 main.dart 调用它。
我有一个包含此代码的单独小部件
class PageEntryWidgetMain extends StatefulWidget {
final PageViewEntryMain pageViewEntryMain;
const PageEntryWidgetMain({Key key, this.pageViewEntryMain})
: super(key: key);
@override
State<StatefulWidget> createState() {
return _PageEntryWidgetMainState();
}
}
class _PageEntryWidgetMainState extends State<PageEntryWidgetMain> {
@override
Widget build(BuildContext context) {
return Container(
child: Row(
children: <Widget>[
Text(widget.pageViewEntryMain.title)
],
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用带有以下代码的视图寻呼机来显示它
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
PageView.builder(
itemBuilder: (context, position) {
PageEntryWidgetMain(
pageViewEntryMain: pages[position],
);
},
itemCount: pages.length,
scrollDirection: Axis.horizontal,
)
],
),
),
);
Run Code Online (Sandbox Code Playgroud)
但它给了我以下错误 …
我有以下变量
String _warningMessage;
bool _warningVisibility;
Run Code Online (Sandbox Code Playgroud)
我想通过实现接口的类进行更新
class _UserSignupInterface extends _SignupSelectUsernamePageState
implements UserSignupInterface {
@override
void onSuccess() {
_hideWarning();
_navigateToUserPage();
}
@override
void onError(String message) {
_isSignupClickable = true;
if(message != null) {
_displayWarning(message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用 _displayWarning 代码(位于 内_SignupSelectUsernamePageState)
void _displayWarning(String message) {
if (message != null) {
setState(() {
widget._warningMessage = message;
widget._warningVisibility = true;
});
}
}
Run Code Online (Sandbox Code Playgroud)
_displayWarning(message)但是,每当我从外部调用_SignupSelectUsernamePageState. 我收到一条错误消息
Unhandled Exception: setState() called in constructor
Run Code Online (Sandbox Code Playgroud)
是否有正确的方法在类之外更新这些变量状态?_displayWarning(message)在我的例子中,我从另一个实现接口的类调用
我有以下政策来确定用户是否能够查看合同。
public function view(User $user, Contract $contract)
{
if ($user->user_type->id == 2) { // If user is a vecino
if ($user->id == $contract->customer_id) {
return true;
}
} else if ($user->user_type->is_admin == true) { // If user is an admin
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
然后检查授权
$this->authorize('view', $contract);
Run Code Online (Sandbox Code Playgroud)
如何检查列表/数组/集合的授权?就像我通过以下方式获得合同清单一样Contract::all()
我还没有找到任何方法来做到这一点。我可以做一个循环并为每次迭代调用 $this->authorize 来检查授权,但这可能会影响性能。
有没有更好的方法来做到这一点?
有没有办法在 dart/flutter 中实现接口而无需使用类?
目前,我的实现方式是使用下面的代码
class _UserSignupInterface extends _SignupSelectUsernamePageState
implements UserSignupInterface {
@override
void onSuccess() {
_navigateToUserPage();
}
@override
void onError() {
setState(() {
_isSignupClickable = true;
});
}
}
_attemptSignup() {
UserSingleton userSingletonInstance = UserSingleton().getInstance();
UserSignupInterface _userSignupInterface = _UserSignupInterface();
UserSingleton().getInstance().user.username = _username;
UserLoginController.attemptSignup(_userSignupInterface,
userSingletonInstance.user, userSingletonInstance.userDetail, _groupID);
}
Run Code Online (Sandbox Code Playgroud)
但是,我想实现这些接口方法而不必使用类,就像在 java 中一样。类似于下面的代码。
UserController.attemptSignup(context, new UserSignupRequest() {
@Override
public void onSuccess(User user, UserDetail userDetail, Group group) {
btnContinueWithFacebook.setEnabled(true);
Intent intent = new Intent(context, ScoopActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
progressBar.setVisibility(View.GONE);
startActivity(intent);
}
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个包含多个密钥对值的对象。
我使用的值将来自 API,我正在尝试将响应转换为单个对象。
我的代码:
json_users.forEach(function (user){
var user_name = user.name;
var object_user = {user_name: null};
temp_collection.push(object_user);
})
Run Code Online (Sandbox Code Playgroud)
我想要的结果
{"key_1":val_1, "key_2", val_2}
Run Code Online (Sandbox Code Playgroud)
我得到的结果
[{
0: {
.....
},
1: {
.....
}
}]
Run Code Online (Sandbox Code Playgroud)