[英]How to show call log in a ListView with Flutter(Dart)
I have added call_log: ^2.0.2
dependency in my flutter app.我在我的 flutter 应用程序中添加了
call_log: ^2.0.2
依赖项。 But cannot get call log and insert into a List View.但无法获取通话记录并插入列表视图。 How can I get call log and add those into a List View?
如何获取通话记录并将其添加到列表视图中?
Here is my code.这是我的代码。 I want to insert all call log to this list view.
我想将所有通话记录插入此列表视图。 I have created three tab and each tab is containing list view.
我创建了三个选项卡,每个选项卡都包含列表视图。 I want to insert call log to the first tab and message to the second tab.
我想将通话记录插入第一个选项卡,并将消息插入第二个选项卡。
import 'package:call_log/call_log.dart';
import 'package:flutter/material.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
Iterable<CallLogEntry> _callLogEntries = [];
@override
Widget build(BuildContext context) {
var mono = TextStyle(fontFamily: 'monospace');
var children = <Widget>[];
_callLogEntries.forEach((entry) {
children.add(
Column(
children: <Widget>[
Divider(),
Text('F. NUMBER: ${entry.formattedNumber}', style: mono),
Text('NUMBER : ${entry.number}', style: mono),
Text('NAME : ${entry.name}', style: mono),
Text('TYPE : ${entry.callType}', style: mono),
Text(
'DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.timestamp)}',
style: mono),
Text('DURATION : ${entry.duration}', style: mono),
],
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
),
);
});
return MaterialApp(
home: DefaultTabController(
length: 3,
child: Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
elevation: 5,
backgroundColor: Colors.black,
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.call)),
Tab(icon: Icon(Icons.message)),
Tab(icon: Icon(Icons.location_on)),
],
),
title: Text('Device Monitor'),
),
body: TabBarView(
children: [
Scrollbar(
child: ListView(
padding: const EdgeInsets.symmetric(vertical: 8),
children: [
Icon(Icons.call),
for (int index = 1; index < 21; index++)
ListTile(
leading: ExcludeSemantics(
child: CircleAvatar(child: Text('$index')),
),
title: Text('item $index'),
),
],
),
),
Scrollbar(
child: ListView(
padding: const EdgeInsets.symmetric(vertical: 8),
children: <Widget>[Icon(Icons.message), Text('test')],
),
),
Icon(Icons.location_on),
],
),
),
),
);
}
}
You didn't get the call log.你没有收到通话记录。 Instead, you assigned your _callLogEntries to [] (an empty array).
相反,您将 _callLogEntries 分配给 [] (一个空数组)。
A good way of loading the call log in a ListView is using FutureBuilder.在 ListView 中加载通话记录的一个好方法是使用 FutureBuilder。
FutureBuilder(
future: CallLog.get(),
builder: (context, snapshot){
if(!snapshot.hasData) return Center(child: CirculaProgressIndicator());
List<CallLogEntry> entries = snapshot.data;
return ScrollBar(
child: ListView.builder(
itemBuilder: (contex, index){
var entry = entries[index];
return Column(
children: <Widget>[
Divider(),
Text('F. NUMBER: ${entry.formattedNumber}', style: mono),
Text('NUMBER : ${entry.number}', style: mono),
Text('NAME : ${entry.name}', style: mono),
Text('TYPE : ${entry.callType}', style: mono),
Text('DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.timestamp)}', style: mono),
Text('DURATION : ${entry.duration}', style: mono),
],
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
);
},
itemCount: entries.length,
),
);
)
Use FutureBuilder will be easy way and you should add.toList() after snapshot.data使用 FutureBuilder 将是一种简单的方法,您应该在 snapshot.data 之后添加.toList()
FutureBuilder(
future: CallLog.get(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Center(child: CircularProgressIndicator());
List<CallLogEntry> entries = snapshot.data.toList();
return Scrollbar(
child: ListView.builder(
itemBuilder: (context, index) {
var entry = entries[index];
var mono = TextStyle(fontFamily: 'monospace');
return Column(
children: <Widget>[
Divider(),
Text('F. NUMBER: ${entry.formattedNumber}',
style: mono),
Text('NUMBER : ${entry.number}', style: mono),
Text('NAME : ${entry.name}', style: mono),
Text('TYPE : ${entry.callType}', style: mono),
Text(
'DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.timestamp)}',
style: mono),
Text('DURATION : ${entry.duration}',
style: mono),
],
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
);
},
itemCount: entries.length,
),
);
}),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.