How to get Text from TextField on button click?

This Article is posted by seven.srikanth at 1/3/2020 4:38:08 AM



This can be achieved by adding a controller to the TextField of type TextEditingController .
Here is how the output is going to look.
Here is an example of how to get the text from the TextField. You can just copy and paste this code to your main.dart file and run to see the output. 
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter TextField Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter TextField Demo'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  String _inputtext;
  TextEditingController inputtextField = TextEditingController();
  void _processText() {
    setState(() {
      _inputtext = inputtextField.text;
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: TextField(
                controller: inputtextField,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  labelText: 'Enter some text'
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: Text(
                '$_inputtext',
                style: Theme.of(context).textTheme.display1,
              ),
            ),
            RaisedButton(
              onPressed: _processText,
              child: Text('Process Text'),
            )
          ],
        ),
      ),
    );
  }
}
Hope this is helpful to you.
Thanks,
Srikanth

Tags:








1 Comments
Login to comment.
Recent Comments

pc_systemx1 at 3/26/2024

Hello friend, I have corrected the code today 2024: import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter TextField Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Flutter TextField Demo'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { String _inputtext=''; TextEditingController inputtextField = TextEditingController(); void _processText() { setState(() { _inputtext = inputtextField.text; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Padding( padding: const EdgeInsets.all(16.0), child: TextField( controller: inputtextField, decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Enter some text' ), ), ), Padding( padding: const EdgeInsets.all(16.0), child: Text( '$_inputtext', style: Theme.of(context).textTheme.headline5, ), ), ElevatedButton( onPressed: _processText, child: Text('Process Text'), ) ], ), ), ); } }

Login to Reply.












© 2018 - Fluttercentral | Email to me - seven.srikanth@gmail.com