(Flutter)输入文本框
CODE
import 'package:flutter/material.dart';
void main() {
return runApp(MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _textController = TextEditingController();
String userPosts = "";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Padding(
padding: const EdgeInsets.all(0.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Expanded(
child: Container(
color: Colors.grey,
child: Center(
child: Text(
userPosts,
style: TextStyle(
color: Colors.white,
fontSize: 50,
),
),
),
),
),
Padding(
padding: EdgeInsets.all(8),
child: TextField(
controller: _textController,
decoration: InputDecoration(
hintText: "Enter text",
border: OutlineInputBorder(),
suffixIcon: IconButton(
onPressed: () {
_textController.clear();
},
icon: Icon(Icons.clear),
),
),
),
),
Padding(
padding: EdgeInsets.all(8),
child: MaterialButton(
color: Colors.blue,
onPressed: () {
setState(
() {
userPosts = _textController.text;
},
);
},
child: Text(
"Submit",
style: TextStyle(
color: Colors.white,
),
),
),
),
],
),
),
);
}
}
预览

License:
CC BY 4.0