TextFormField數據處理


重點:TextFormField這個Widget是由TextField封裝而來,繼承了TextField的特性:
數據傳遞依靠:
GlobalKey<FormState>(),
RegisterKey.currentState.save();
加上Form里面有key選項,這三項復合起來,可以達到數據傳遞的目的。具體請看如下示例:


import 'package:flutter/material.dart';


void main()=>runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: HomePage(),
),
);
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
final RegisterKey=GlobalKey<FormState>();
String username, password;

void submitForm(){
RegisterKey.currentState.save();
debugPrint('username:$username');
debugPrint('password:$password');
}

@override
Widget build(BuildContext context) {
return Form(
key:RegisterKey,
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.all(10.0),
child: TextFormField(
decoration: InputDecoration(
labelText: 'Please input username',
contentPadding: EdgeInsets.all(20),
),
onSaved: (value){
username=value;
},
),
),
Container(
padding: EdgeInsets.all(10.0),
child: TextFormField(
obscureText: true,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(20),
labelText: 'Please input password',
),
onSaved: (value){password=value;},
),
),
SizedBox(height: 10,),
Container(
padding: EdgeInsets.all(20.0),
width: double.infinity,
child: RaisedButton(
onPressed:submitForm,
color: Colors.lightGreen,
child: Text('點我哦'),
),
),
],
),
);
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM