Create a mobile application that will calculate the number of words containing the letter "A" in a text entered by the user. I try but dont work. I think problem is that
onChanged: (String str) {
int temp = 0;
List<String> ar = str.split(" ");
temp = ar.length;
setState(() {
_wordcount = temp;
});
import 'package:flutter/material.dart';
void main() => runApp(Myapp());
class Myapp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement buildthrow UnimplementedError();
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blueAccent,
title: Text(
"Laboratorul 2",
style: TextStyle(
color: Colors.white,
),
),
),
body: WordCounter(),
),
);
}
}
class WordCounter extends StatefulWidget {
@override
_WordCounter createState() => _WordCounter();
}
class _WordCounter extends State<WordCounter> {
int _wordcount = 0;
@override
Widget build(BuildContext context) {
// TODO: implement build
return Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.all(8),
child: TextField(
decoration: InputDecoration(
labelText: "Introdu textul aici...",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5.0),
)),
onChanged: (String str) {
int temp = 0;
List<String> ar = str.split(" ");
temp = ar.length;
setState(() {
_wordcount = temp;
});
},
),
),
Padding(
padding: EdgeInsets.all(8),
child: Text(
"Numarul de cuvinte sunt : " _wordcount.toString(),
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 25,
),
),
),
],
),
);
}
}
CodePudding user response:
try this:
onChanged: (String str) {
int temp = 0;
List<String> ar = str.split(" ").where((word) => word.contains("A")).toList();
temp = ar.length;
setState(() {
_wordcount = temp;
});
},