2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Flutter Dialogueの中でState管理する方法

Posted at

使用例: アカウント削除時に削除前にパスワードを求め、認証してから削除する時にパスワードの入力状態によって削除ボタンをenableしたりする場合。

StatefulBuilderを使用して、Dialog内でsetStateを使用し、Dialog内でのみWidgetsを更新します。

Widget _buildSubmitButton() {
    return ElevatedButton(
      onPressed: _isButtonEnabled ? _deleteConfirmDialogue : null,
    );
  }

  Widget _enterPassword() {
    return StatefulBuilder <- StatefulBuilderで包んであげる
      builder: (context, setState) {
        return Container(
          child: SimpleDialog(
            title: Text(
              'Enter password',
              textAlign: TextAlign.center,
            ),
            children: [
              Container(
                padding: EdgeInsets.all(15),
                child: Column(
                  children: [
                    Text("please enter your password to proceed"),
                    TextField(
                      keyboardType: TextInputType.visiblePassword,
                      decoration: InputDecoration(
                        // border: InputBorder.none,
                        icon: Icon(
                          Icons.lock,
                          color: Color(0xff6bceff),
                        ),
                        hintText: 'password',
                      ),
                      onChanged: (value) {
                        setState(() {
                          password = value;
                          _isButtonEnabled = password.length > 0;
                        });
                      },
                    ),
                    SizedBox(height: 10),
                    _buildSubmitButton(),
                  ],
                ),
              )
            ],
          ),
        );
      },
    );
  }

Screen Shot 2021-05-05 at 16.27.41.png

Screen Shot 2021-05-05 at 16.27.46.png

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?