-
-
Save ProZhar/622a37503e10941af7e6f944829b7925 to your computer and use it in GitHub Desktop.
Clear text field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(const MyApp()); | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return const MaterialApp( | |
title: 'Limpando a caixa de texto', | |
home: MyCustomForm(), | |
); | |
} | |
} | |
// Define a custom Form widget. | |
class MyCustomForm extends StatefulWidget { | |
const MyCustomForm({super.key}); | |
@override | |
State<MyCustomForm> createState() => _MyCustomFormState(); | |
} | |
class _MyCustomFormState extends State<MyCustomForm> { | |
final myController = TextEditingController(); | |
@override | |
void dispose() { | |
myController.dispose(); | |
super.dispose(); | |
} | |
void setFilter(value) { | |
print('Set filter: $value'); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Retrieve Text Input'), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Column( | |
children: [ | |
TextField( | |
controller: myController, | |
onChanged: setFilter, | |
decoration: InputDecoration( | |
suffixIcon: IconButton( | |
icon: const Icon(Icons.clear), | |
onPressed: () => myController.text = '', | |
), | |
), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment