Skip to content

Instantly share code, notes, and snippets.

@tetujin
Created December 25, 2021 07:55
Show Gist options
  • Save tetujin/1e57c0ebd6427cb007b5ea799a94caf8 to your computer and use it in GitHub Desktop.
Save tetujin/1e57c0ebd6427cb007b5ea799a94caf8 to your computer and use it in GitHub Desktop.
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),
),
],
);
});
}
}
@PhanDuyDLCT
Copy link

THANKS SO MUCH

@holocronweaver
Copy link

holocronweaver commented Oct 6, 2022

+1, this was very helpful. Wasn't aware of TextEditingController.

@ronanwp
Copy link

ronanwp commented Dec 30, 2023

これはとても役に立ちました! どうもありがとう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment