How insert text to list from text field and then text field will clear when added by button?
List texts = []
Insert from text field to list after pressing onChanged funciton.
How insert text to list from text field and then text field will clear when added by button?
List texts = []
Insert from text field to list after pressing onChanged funciton.
Let's try
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:so_test/screen/exapmple_page.dart';
void main() => runApp(const MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: MyStatefulWidget(),
),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool selected = false;
List<String> text = [];
TextEditingController _controller = TextEditingController();
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: BoxDecoration(color: Colors.white12),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
TextFormField(
controller: _controller,
),
TextButton(
onPressed: () {
setState(() {
if(_controller.text.length>0){
text.add(_controller.text);
_controller.clear();
} else{
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text("Text is empty"),
));
}
});
},
child: Text("add"),
),
Text(text.isNotEmpty?text.toString():""),
],
),
),
);
}
}
on the onTap or onPressed you could do something like this:
onTap:(){
texts.add(_textController.text);
_textController.clear();
}
Note: You have not provided enough information in this question, so I am assuming that your TextController name is _textController
try to send the data on onSubmit if you done typing
onSubmit:(String value){
print(value);
text.add(value);
}
Here you are doing 2 thing
1> Appending or adding value to the list
2> Clearing the text filed
Here you want to use TextEditingController This help to access the test from the textfield. Declare TextTditingcontroller by : -
TextEditingController Input = TextEditingController();
NB : controller: Input inside your TextFormField
1> For adding to list
Add this inside your onSubmit or Ontap function
List.add("Input.text");
2> For clearing the text filed
setState(() {
inputController.text = "";
});
Put this inside your onSubmit Function.
Make sure you are using stateful widget
Thank you
You can use the TextEditingController.
You need to create an instance of it and pass it to the TextField in this way.
TextEditingController _instance = TextEditingController();
TextField(controller: _instance);
And then when you press your Button you should do something like
Button(
onPressed: (){
List.add(_instance.text);
_instance.text = '';
setState((){});
}
)
Note that the TextEditingController should be created once