Last active
July 25, 2024 13:39
-
-
Save BbsonLin/472f481cb95c2c58d4558f6b7d1c414c to your computer and use it in GitHub Desktop.
Flutter showDialog with return value
This file contains hidden or 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
import 'package:flutter/material.dart'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.light().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
TextEditingController _controller = TextEditingController(); | |
String inputString = ""; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Text(inputString), | |
ElevatedButton( | |
child: Text("Show Dialog"), | |
onPressed: () { | |
showDialog( | |
context: context, | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: Text("Setting String"), | |
content: TextFormField( | |
controller: _controller, | |
), | |
actions: <Widget>[ | |
TextButton( | |
child: Text("OK"), | |
onPressed: () { | |
Navigator.pop(context, _controller.text); | |
}, | |
) | |
], | |
); | |
}, | |
).then((val) { | |
setState(() { | |
inputString = val; | |
}); | |
}); | |
}, | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DartPad Demo: dartpad.dev/?id=472f481cb95c2c58d4558f6b7d1c414c.
Based on Dart SDK 3.4.4 and Flutter SDK 3.22.3