簡體   English   中英

從 TextFormField 檢索到的文本未顯示在 Flutter 中

[英]Text retrieved from TextFormField is not showing in Flutter

我試圖通過TextField獲取客戶編號,只是為了測試在下一個屏幕上顯示它,但沒有出現。

Widget build(BuildContext context) {
  final searchRecordField = TextFormField(
    autofocus: false,
    keyboardType: TextInputType.number,
    controller: customerNumber,
    textInputAction: TextInputAction.next,
    decoration: InputDecoration(
      prefixIcon: Icon(Icons.phone),
      contentPadding: EdgeInsets.fromLTRB(20, 15, 20, 15),
      hintText: "Enter Customer Number",
      border: OutlineInputBorder(
        borderRadius: BorderRadius.circular(10),
      ),
    ),
  );

下一個屏幕的代碼。

return Scaffold(
    body: Container(
      child: (
        Text("Showing Results for ${customerNumber.text.toString()}",
        style: TextStyle(
          color: Colors.black12, fontSize: 24, fontWeight: FontWeight.bold),
      ),
    ),
  ),
);

它顯示“正在顯示結果”但不是我在上一個屏幕中輸入的數字。

您必須將數據從初始屏幕傳遞到新屏幕,以便您可以呈現數據。

這是有關如何在 2 個屏幕之間傳遞數據的 flutter 資源。

https://docs.flutter.dev/cookbook/navigation/navigate-with-arguments

文檔中,

您可以使用Navigator.pushNamed()方法的 arguments 參數完成此任務。 使用ModalRoute.of()方法或在提供給MaterialAppCupertinoApp構造函數的onGenerateRoute() function 中提取 arguments。

下面是一個應該可以解決問題的簡單原型。 檢查屏幕截圖和DartPad 原型

第一頁 第二頁
第一屏 第二屏
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: '/home',
      routes: {
        '/home': (context) => const HomePage(),
        '/next': (context) => NextPage(
              ModalRoute.of(context)!.settings.arguments as String,
            ),
      },
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final customerNumber = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Pass Args Between Routes Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.all(20),
              child: TextFormField(
                style: Theme.of(context).textTheme.headline4,
                autofocus: false,
                keyboardType: TextInputType.number,
                controller: customerNumber,
                textInputAction: TextInputAction.next,
                decoration: InputDecoration(
                  prefixIcon: const Icon(Icons.phone),
                  contentPadding: const EdgeInsets.fromLTRB(20, 15, 20, 15),
                  hintText: "Enter Customer Number",
                  border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(10),
                  ),
                ),
              ),
            ),
            ElevatedButton(
                onPressed: () => Navigator.pushNamed(context, '/next',
                    arguments: customerNumber.text),
                child: const Text(
                  'Next',
                  style: TextStyle(fontSize: 30),
                ))
          ],
        ),
      ),
    );
  }
}

class NextPage extends StatefulWidget {
  final String customerNumber;
  const NextPage(this.customerNumber, {Key? key}) : super(key: key);

  @override
  State<NextPage> createState() => _NextPageState();
}

class _NextPageState extends State<NextPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Pass Args Between Routes Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.all(20),
              child: Text(
                widget.customerNumber,
                style: const TextStyle(fontSize: 30),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM