Created
January 29, 2020 00:26
-
-
Save ChangJoo-Park/8992c2882bdb895abfb268563edd0426 to your computer and use it in GitHub Desktop.
Using State in Dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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