【发布时间】:2021-04-16 20:03:38
【问题描述】:
我试图让用户搜索并且效果很好,但现在我希望与结果相同的搜索放置字符是 Fontweight 粗体。但为此苦苦挣扎。 这是我的代码
class Openmyprofil extends StatefulWidget {
final TextEditingController searchinginput;
const Openmyprofil({Key key, this.searchinginput}) : super(key: key);
@override
_OpenmyprofilState createState() => _OpenmyprofilState();
}
class _OpenmyprofilState extends State<Openmyprofil> {
List _allResults = [];
List _resultsList = [];
Future resultsLoaded;
bool nosuerfound = false;
var titles;
@override
void initState() {
super.initState();
widget.searchinginput.addListener(_onsearchChanged);
setState(() {
nosuerfound = true;
});
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
resultsLoaded = getusers();
}
_onsearchChanged() {
print(_resultsList);
print(_allResults);
print(searchResults);
setState(() {
nosuerfound = false;
});
searchResults();
}
@override
void dispose() {
widget.searchinginput.removeListener(_onsearchChanged());
super.dispose();
}
searchResults() {
var showResults = [];
if (widget.searchinginput.text != "") {
for (var tripsnapshot in _allResults) {
titles = tripsnapshot;
var title = DatbaseService.instance
.userDataFromSnapshot(tripsnapshot)
.username
.toLowerCase();
if (title.contains(widget.searchinginput.text.toLowerCase())) {
setState(() {
nosuerfound = true;
});
showResults.add(tripsnapshot);
print(tripsnapshot);
}
}
} else {
setState(() {
nosuerfound = true;
});
showResults = List.from(_allResults);
}
setState(() {
_resultsList = showResults;
});
}
getusers() async {
var firestore = FirebaseFirestore.instance;
QuerySnapshot qn = await firestore.collection('meinprofilsettings').get();
setState(() {
_allResults = qn.docs;
});
searchResults();
return "Complete";
}
@override
Widget build(BuildContext context) {
final user = Provider.of<Userforid>(context);
if (nosuerfound == true) {
return Container(
child: ListView.builder(
itemCount: _resultsList.length,
itemBuilder: (BuildContext context, int index) {
return ListTile(
onTap: () {
DatbaseService.instance.createorGetConversation(
user.uid, _resultsList[index].id,
(String _conversationID) {
NavigationService.instance.navigateToRoute(
MaterialPageRoute(builder: (context) {
return MeineBeitraege(
_conversationID,
_resultsList[index].id,
_resultsList[index].data()['username'],
_resultsList[index].data()['url'],
_resultsList[index].data()['email']);
}),
);
});
},
leading: Container(
child: ClipRRect(
borderRadius: BorderRadius.circular(60),
child: Container(
height: 50,
width: 50,
decoration: BoxDecoration(
color: Colors.white,
),
child: _resultsList[index].data()['url'] != null &&
_resultsList[index].data()['url'] !=
"profilepictureer"
? Image.network(
_resultsList[index].data()['url'],
fit: BoxFit.cover,
)
: Image.asset(
'assets/profilepictureer.png') // Your widget is here when image is no available.
),
),
decoration: new BoxDecoration(
shape: BoxShape.circle,
border: new Border.all(color: Colors.black, width: 4),
),
),
title: Text(_resultsList[index].data()['username'],
style: widget.searchinginput.text.contains(
_resultsList[index].data()['username']) ||
widget.searchinginput.text.contains(
_resultsList[index].data()['username'])
? TextStyle(fontWeight: FontWeight.bold)
: null),
subtitle: Text(_resultsList[index].data()['email']),
);
}));
} else {
return Padding(
padding: const EdgeInsets.fromLTRB(0, 30, 0, 0),
child: Container(
child: Text(
"No user found",
style: TextStyle(fontSize: 16),
)),
);
}
}
}
所以 widget.searchinginput 是我的 TextEditingController 我想要的就是如果用户输入 Mi 我在 _resultlist 中有一个名字 Mike,然后 Mike 的 Mi 应该是粗体,然后用户输入 Mik ,Mik in resultless 应该是大胆的希望任何人都可以提供帮助。 如果您需要更多信息,请发表评论
【问题讨论】:
-
这有点复杂,但要使某些部分加粗而另一些不加粗,您需要使用 Text.rich。我建议使用 RegExp 来查找应该加粗的部分。能否请您更好地格式化代码以便我可以帮助您?
-
我编辑它请检查
-
好的。我正在尝试在发送代码之前使代码正常工作。等一下……