Merahaba arkadaşlar. Elimdeki hazır map listesindeki color key ine göre arama yapıp eğer color’ ı aynı olan bir map e rastlarsa sadece o map in verilerini, eğer aynı color değerini bulamazsa da map listesinin tamamını list view olarak aşağıda gösteren bir örnek yazmaya çalıştım ama bahsettiğim hatayı aldım. Fikri olan varsa paylaşırsa sevinirim. Teşekkürler.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: MyPage(),
),
);
}
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> {
TextEditingController _textEditingController = TextEditingController();
List<Map> strglist = [];
String str;
int index=0;
List<Map> maplist = [
{"color": "red", "value": "#f00"},
{"color": "green", "value": "#0f0"},
{"color": "blue", "value": "#00f"},
{"color": "cyan", "value": "#0ff"},
{"color": "magenta", "value": "#f0f"},
{"color": "yellow", "value": "#ff0"},
{"color": "black", "value": "#000"}
];
void search() {
setState(() {
str = _textEditingController.text;
while (index < maplist.length) {
if (str == maplist[index]["color"]) {
strglist[0]["color"] = str;
strglist[0]["value"] = maplist[index]["value"];
} else {
index++;
}
}
});
}
@override
void dispose() {
_textEditingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
"AppBar",
),
centerTitle: true,
),
body: Container(
padding: EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
child: TextField(
controller: _textEditingController,
style: TextStyle(
color: Colors.white,
fontSize: 25.0,
),
textAlign: TextAlign.center,
decoration: InputDecoration(
hintText: "Enter input value",
filled: true,
fillColor: Colors.black,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20.0),
),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.all(20.0),
child: RaisedButton(
color: Colors.black,
child: Text(
"Search Item",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
textAlign: TextAlign.center,
),
splashColor: Colors.grey[700],
shape: RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(18.0),
side: BorderSide(color: Colors.black),
),
onPressed: () {
search();
setState(() {});
},
),
),
],
),
Expanded(
child: strglist.length == 0
? ListView.builder(
itemCount: maplist.length,
itemBuilder: (context, item) {
return ListTile(
tileColor: Colors.grey[800],
title: Text(
"color => ${maplist[item]["color"]} value => ${maplist[item]["value"]}",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
),
);
})
: ListView.builder(
itemCount: maplist.length,
itemBuilder: (context, item) {
return ListTile(
tileColor: Colors.green[700],
title: Text(
"color => ${strglist[item]["color"]} value => ${strglist[item]["value"]}",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 20.0,
),
),
);
}),
),
],
),
),
);
}
}