【发布时间】:2020-09-16 15:07:39
【问题描述】:
我是 Flutter 的新手,我有一个如下所示的本地 json 文件,设法在我的 UI 中显示它,并且还显示了过滤器/搜索栏,但现在我正在尝试制作过滤器列表视图功能。我尝试了几件事,但没有成功。如何将用户输入与我的 JSON 文件中的数据进行比较?我想知道如何使搜索功能适用于“名称”?
{
"name": "Telekinesis",
"description": "Rubick uses his telekinetic powers to lift the enemy into the air briefly and then hurls them back at the ground.",
"logo_url": "http://static.wikia.nocookie.net/dota2_gamepedia/images/0/0e/Telekinesis_icon.png"
},
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(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Header'),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
TextEditingController controller = new TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(child: Text(widget.title)),
),
body: Container(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
decoration: InputDecoration(
hintText: "Search Title",
border: OutlineInputBorder(
borderSide:
BorderSide(color: Theme.of(context).primaryColor))),
),
),
Expanded(
child: FutureBuilder(
future: DefaultAssetBundle.of(context)
.loadString('components/abilities-list.json'),
builder: (context, snapshot) {
var myData = jsonDecode(snapshot.data.toString());
return ListView.builder(
itemBuilder: (BuildContext context, index) {
return ListTile(
leading: CircleAvatar(
backgroundImage:
NetworkImage(myData[index]['logo_url'])),
title: Text('Name: ' + myData[index]['name']),
subtitle: Text(
'Description: ' + myData[index]['description'],
));
},
itemCount: myData == null ? 0 : myData.length,
);
},
),
),
],
),
),
);
}
}
【问题讨论】: