Skip to content

Instantly share code, notes, and snippets.

@pratikbutani
Created April 3, 2021 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pratikbutani/b5654c6a8924ba6c8eb208e6592522f4 to your computer and use it in GitHub Desktop.
Save pratikbutani/b5654c6a8924ba6c8eb208e6592522f4 to your computer and use it in GitHub Desktop.
Flutter onChange of TextField getting call on keyboard hide (via back press)
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _value = "Here we go...";
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
child: Column(
children: [
SizedBox(height: 20),
TextField(
decoration: new InputDecoration(
labelText: "Enter something to reflect",
fillColor: Colors.white,
border: new OutlineInputBorder(
borderRadius: new BorderRadius.circular(15.0),
borderSide: new BorderSide(),
),
),
onChanged: (value) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("${"Changed Value : "} : " + value),
duration: Duration(milliseconds: 100),
));
setState(() {
_value += "\n" + value;
});
},
keyboardType: TextInputType.streetAddress,
),
SizedBox(height: 20),
Expanded(flex: 1, child: SingleChildScrollView(child: Text(_value))),
],
),
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment