Created
December 25, 2021 07:55
-
-
Save tetujin/1e57c0ebd6427cb007b5ea799a94caf8 to your computer and use it in GitHub Desktop.
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
import 'package:flutter/material.dart'; | |
/////////////////////////////// | |
void main() => runApp(MyApp()); | |
/////////////////////////////// | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp(theme: ThemeData(), home: MyHomePage()); | |
} | |
} | |
/////////////////////////////// | |
class MyHomePage extends StatefulWidget { | |
List<Widget> cards = []; | |
MyHomePage({Key? key}) : super(key: key); | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
String? label = ""; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text("My TODO"), | |
), | |
body: Center( | |
child: Text(label ?? "") | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: () async { | |
var resultLabel = await _showTextInputDialog(context); | |
if (resultLabel != null) { | |
setState((){ | |
label = resultLabel; | |
}); | |
} | |
}, | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
final _textFieldController = TextEditingController(); | |
Future<String?> _showTextInputDialog(BuildContext context) async { | |
return showDialog( | |
context: context, | |
builder: (context) { | |
return AlertDialog( | |
title: const Text('TODO'), | |
content: TextField( | |
controller: _textFieldController, | |
decoration: const InputDecoration(hintText: "タスクの名称を入力してください。"), | |
), | |
actions: <Widget>[ | |
ElevatedButton( | |
child: const Text("キャンセル"), | |
onPressed: () => Navigator.pop(context), | |
), | |
ElevatedButton( | |
child: const Text('OK'), | |
onPressed: () => Navigator.pop(context, _textFieldController.text), | |
), | |
], | |
); | |
}); | |
} | |
} |
+1, this was very helpful. Wasn't aware of TextEditingController.
これはとても役に立ちました! どうもありがとう。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
THANKS SO MUCH