Skip to content

Instantly share code, notes, and snippets.

@ChangJoo-Park
Created January 29, 2020 00:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChangJoo-Park/8992c2882bdb895abfb268563edd0426 to your computer and use it in GitHub Desktop.
Save ChangJoo-Park/8992c2882bdb895abfb268563edd0426 to your computer and use it in GitHub Desktop.
Using State in Dialog
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool checked = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Open AlertDialog'),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text("CheckBox"),
actions: <Widget>[
Row(
children: [
Row(
children: [
Checkbox(
value: checked,
onChanged: (bool value) {
setState(() {
checked = value;
});
},
),
Text('Agree'),
],
),
FlatButton(
onPressed: () => Navigator.pop(context),
child: Text("Dismiss"),
),
],
),
],
);
},
);
},
);
},
),
RaisedButton(
child: Text('Open SimpleDialog'),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
return CheckBoxAlertDialog();
},
);
},
),
],
),
),
);
}
}
class CheckBoxAlertDialog extends StatefulWidget {
CheckBoxAlertDialog({Key key}) : super(key: key);
@override
_CheckBoxAlertDialogState createState() => _CheckBoxAlertDialogState();
}
class _CheckBoxAlertDialogState extends State<CheckBoxAlertDialog> {
bool checked = false;
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text("CheckBox"),
actions: <Widget>[
Row(
children: [
Row(children: [
Checkbox(
value: checked,
onChanged: (bool value) {
setState(() {
checked = value;
});
},
),
Text('Agree'),
]),
FlatButton(
onPressed: () => Navigator.pop(context),
child: Text("Dismiss"),
),
],
),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment