我是新來的。我僅從 flutter.dev 網站學習帶有驗證邏輯的 Flutter 表單。當我將整個示例代碼復制到我的 Android 作業室并運行代碼時,出現了一個非常奇怪的輸出,它沒有顯示任何所需的表單詳細資訊。為什么實際輸出沒有出現?輸出的螢屏截圖與要點鏈接代碼一起附加。
輸出 SS
代碼的要點鏈接
uj5u.com熱心網友回復:
您可以使用此代碼將表單驗證演示顯示為輸出螢屏。對于表單驗證,您還可以使用此插件form_field_validator:^1.1.0
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) {
const appTitle = 'Form Validation Demo';
return MaterialApp(
title: appTitle,
home: Scaffold(
appBar: AppBar(
title: const Text(appTitle),
),
body: const MyCustomForm(),
),
);
}
}
// Create a Form widget.
class MyCustomForm extends StatefulWidget {
const MyCustomForm({Key? key}) : super(key: key);
@override
MyCustomFormState createState() {
return MyCustomFormState();
}
}
// Create a corresponding State class.
// This class holds data related to the form.
class MyCustomFormState extends State<MyCustomForm> {
// Create a global key that uniquely identifies the Form widget
// and allows validation of the form.
//
// Note: This is a GlobalKey<FormState>,
// not a GlobalKey<MyCustomFormState>.
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
// The validator receives the text that the user has entered.
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, or false otherwise.
if (_formKey.currentState!.validate()) {
// If the form is valid, display a snackbar. In the real world,
// you'd often call a server or save the information in a database.
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Processing Data')),
);
}
},
child: const Text('Submit'),
),
),
],
),
);
}
}
uj5u.com熱心網友回復:
您對我剛剛將您的代碼復制到 dartpad 的代碼沒有任何問題,并且它的作業正常嘗試重新啟動一個新專案
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/405913.html
標籤:
