0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Flutterでテキスト入力を表示する方法

0
Posted at

入力したテキストをそのまま表示してみようと思います。

こちらの遷移後のページに追加してみようと思います。

まず、今まではStatelessWidgetを使用していましたが、こちらを変更します。
StatelessWidgetは名前の通り「状態を持たないWidget」です。
そのため、以下のような処理はできません。

入力された値を保持する
入力に応じてUIを更新する

このような場合はStatefulWidgetを使用します。

StatefulWidgetに変更する

.dart
class SecondPage extends StatefulWidget {
  const SecondPage({super.key});

  @override
  State<SecondPage> createState() => _SecondPageState();
}

テキスト入力と表示を追加

class _SecondPageState extends State<SecondPage> {
  String inputText = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('2ページ目')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
+           TextField(
+             onChanged: (text) {
+               setState(() {
+                 inputText = text;
+               });
+             },
+           ),
+           SizedBox(height: 20),
+           Text(inputText),
+           SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('戻る'),
            ),
          ],
        ),
      ),
    );
  }
}

Simulator Screenshot - iPhone 16 Pro - 2026-04-05 at 14.59.16.png

Screenshot_20260405_150104.png

入力値を扱う場合はStatefulWidgetが必要。
onChanged + setStateでリアルタイム更新ができます。

以上になります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?