我正在开发“在文档中搜索”应用程序。我想在输入单词时自定义滚动视图自动滚动到搜索到的单词。 我将文档拆分为一堆文本跨度。如何获得其中一个的偏移量。那有可能吗?
我尝试了,但是没用
with AutomaticKeepAliveClientMixin<WordViewPage> {
@override
bool get wantKeepAlive => true;
String previewedText;
ScrollController scrollController;
List<String> splitted = [''];
Color customColor = Colors.transparent;
@override
void initState() {
super.initState();
loadAsset().then((String loadedString) {
setState(() {
previewedText = loadedString;
splitter();
});
});
}
@override
Widget build(BuildContext context) {
super.build(context);
List<Widget> he = [wordFile()];
return Scaffold(
body: CustomScrollView(
controller: scrollController,
slivers: <Widget>[
SliverAppBar(
automaticallyImplyLeading: false,
title: searchBar(),
centerTitle: true,
backgroundColor: Color(0xfffc3b398),
),
SliverPadding(
padding: EdgeInsets.all(10),
sliver: SliverList(
delegate: SliverChildListDelegate(he),
),
)
],
));
}
Future<String> loadAsset() async {
return await rootBundle.loadString('assets/test.txt');
}
答案 0 :(得分:0)
您可以通过扑朔迷离的搜索代表来扩展课程
详细说明和代码:SearchDelegate
或者您可以看到我的代码并看到RichText()小部件
@override
Widget buildResults(BuildContext context){
return new FutureBuilder(
future: _getUser(query),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Container(
child: Center(
child: CircularProgressIndicator(),
),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) => ListTile(
leading: Icon(Icons.person),
title: Text(snapshot.data[index].fName +
" " +
snapshot.data[index].lName),
title: RichText(
text: TextSpan(
text: suggestionList[index].substring(0, query.length),
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold)),
),
onTap: () {
},
),
);
}
},
);
}