SearchScreen.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_easyloading/flutter_easyloading.dart';
  4. import 'package:flutter_styled_toast/flutter_styled_toast.dart';
  5. import 'package:prime_chat/screens/ProfileScreen.dart';
  6. import 'package:prime_chat/screens/components/Avatar.dart';
  7. import 'package:prime_chat/screens/components/ItemBuilder.dart';
  8. import '../basic/Method.dart';
  9. import 'components/ProgressDialog.dart';
  10. class SearchScreen extends StatefulWidget {
  11. const SearchScreen({Key? key}) : super(key: key);
  12. @override
  13. State<SearchScreen> createState() => _SearchScreenState();
  14. }
  15. class _SearchScreenState extends State<SearchScreen> {
  16. bool loading = false;
  17. List<Widget> children = [];
  18. @override
  19. Widget build(BuildContext context) {
  20. return ProgressDialog(
  21. loading: loading,
  22. label: "搜索中...",
  23. child: Scaffold(
  24. body: Column(
  25. children: [
  26. SearchBar(
  27. onSubmitted: (value) => _search(value),
  28. ),
  29. ...children
  30. ],
  31. ),
  32. ));
  33. }
  34. _search(String name) async {
  35. setState(() {
  36. loading = true;
  37. });
  38. final profile = await method.getProfile(name: name);
  39. debugPrint("get profile = ${profile?.name}");
  40. setState(() {
  41. loading = false;
  42. if (profile != null) {
  43. children.add(ListTile(
  44. leading: Avatar(profile.avatar),
  45. title: Text(profile.name),
  46. onTap: () {
  47. Navigator.push(
  48. context,
  49. MaterialPageRoute(
  50. builder: (context) =>
  51. ProfileScreen(profile: profile, isFriend: false)));
  52. },
  53. ));
  54. }
  55. });
  56. }
  57. }
  58. class SearchBar extends StatelessWidget {
  59. const SearchBar({Key? key, this.onSubmitted, this.onChanged})
  60. : super(key: key);
  61. final void Function(String)? onSubmitted;
  62. final void Function(String)? onChanged;
  63. @override
  64. Widget build(BuildContext context) {
  65. final theme = Theme.of(context);
  66. return Container(
  67. height: 80,
  68. color: theme.appBarTheme.backgroundColor,
  69. child: Column(
  70. children: [
  71. const SizedBox(
  72. height: 30,
  73. ),
  74. SizedBox(
  75. height: 40,
  76. child: Row(
  77. children: [
  78. Expanded(
  79. child: Container(
  80. // color: Colors.red,
  81. // width: MediaQuery.of(context).size.width - 50,
  82. margin: const EdgeInsets.symmetric(horizontal: 10),
  83. padding: const EdgeInsets.symmetric(horizontal: 10),
  84. decoration: BoxDecoration(
  85. color: Colors.white,
  86. borderRadius: BorderRadius.circular(6.0),
  87. ),
  88. child: TextField(
  89. cursorColor: Colors.green,
  90. autofocus: true,
  91. style: const TextStyle(
  92. fontSize: 18.0,
  93. color: Colors.black,
  94. fontWeight: FontWeight.w300,
  95. ),
  96. decoration: const InputDecoration(
  97. contentPadding: EdgeInsets.only(bottom: 10),
  98. icon: Icon(
  99. Icons.search,
  100. size: 20.0,
  101. color: Colors.grey,
  102. ),
  103. border: InputBorder.none,
  104. hintText: '搜索',
  105. hintStyle: TextStyle(color: Colors.grey),
  106. ),
  107. onSubmitted: onSubmitted,
  108. onChanged: onChanged,
  109. ),
  110. )),
  111. SizedBox(
  112. width: 40,
  113. // height: 34,
  114. child: GestureDetector(
  115. child: const Text("取消"),
  116. onTap: () => Navigator.pop(context),
  117. ))
  118. ],
  119. ),
  120. ),
  121. ],
  122. ),
  123. );
  124. }
  125. }