choose_country.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_svg/svg.dart';
  3. import '../constants.dart';
  4. import '../utils/size_fit.dart';
  5. import 'component/top_container.dart';
  6. class ChooseCountry extends StatefulWidget {
  7. const ChooseCountry({Key? key}) : super(key: key);
  8. @override
  9. State<ChooseCountry> createState() => _ChooseCountryState();
  10. }
  11. class _ChooseCountryState extends State<ChooseCountry> {
  12. String keyword = '';
  13. Widget item() {
  14. List<TextSpan> spans = [];
  15. int start = 0; // 当前要截取字符串的起始位置
  16. int end; // end 表示要高亮显示的文本出现在当前字符串中的索引
  17. String text = '中国中 86';
  18. String lightText = '中';
  19. // 如果有符合的高亮文字
  20. while ((end = text.indexOf(lightText, start)) != -1) {
  21. // 第一步:添加正常显示的文本
  22. spans.add(TextSpan(text: text.substring(start, end), style: TextStyle(color: Colors.white, fontSize: 14.px)));
  23. // 第二步:添加高亮显示的文本
  24. spans.add(TextSpan(text: lightText, style: TextStyle(color: const Color(0xFFFF9900), fontSize: 14.px)));
  25. // 设置下一段要截取的开始位置
  26. start = end + lightText.length;
  27. }
  28. // 下面这行代码的意思是
  29. // 如果没有要高亮显示的,则start=0,也就是返回了传进来的text
  30. // 如果有要高亮显示的,则start=最后一个高亮显示文本的索引,然后截取到text的末尾
  31. spans.add(
  32. TextSpan(text: text.substring(start, text.length), style: TextStyle(color: Colors.white, fontSize: 14.px)),
  33. );
  34. return GestureDetector(
  35. onTap: (){
  36. Navigator.of(context).pop();
  37. },
  38. child: Container(
  39. height: 50.px,
  40. alignment: Alignment.centerLeft,
  41. padding: EdgeInsets.fromLTRB(30.px, 15.px, 30.px, 15.px),
  42. child: RichText(text: TextSpan(children: spans)),
  43. ),);
  44. }
  45. @override
  46. Widget build(BuildContext context) {
  47. SizeFit.initialize(context);
  48. return Material(
  49. color: kBgColor,
  50. child: TopContainer(
  51. child: Stack(children: [
  52. SingleChildScrollView(
  53. child: Column(
  54. children: [
  55. SizedBox(
  56. height: 72.px,
  57. ),
  58. item(),
  59. item(),
  60. item(),
  61. item(),
  62. item(),
  63. item(),
  64. item(),
  65. item(),
  66. item(),
  67. item(),
  68. item(),
  69. item(),
  70. item(),
  71. item(),
  72. item(),
  73. item(),
  74. item(),
  75. item(),
  76. item(),
  77. item(),
  78. ],
  79. ),
  80. ),
  81. Positioned(
  82. left: 0,
  83. right: 0,
  84. bottom: 0,
  85. height: 48.px,
  86. child: Container(
  87. decoration: const BoxDecoration(
  88. gradient: LinearGradient(
  89. begin: Alignment.topCenter,
  90. end: Alignment.bottomCenter,
  91. colors: [
  92. Color(0x00131314),
  93. Color(0xFF131314),
  94. Color(0xFF131314),
  95. ])),
  96. )),
  97. Column(
  98. children: [
  99. Container(
  100. height: 72.px,
  101. padding: EdgeInsets.only(left: 16.px),
  102. decoration: const BoxDecoration(
  103. gradient: LinearGradient(
  104. begin: Alignment.topCenter,
  105. end: Alignment.bottomCenter,
  106. colors: [
  107. Color(0xFF131314),
  108. Color(0xFF131314),
  109. Color(0xFF131314),
  110. Color(0x00131314)
  111. ])),
  112. child: Row(
  113. children: [
  114. Expanded(
  115. child: Container(
  116. height: 40.px,
  117. padding: EdgeInsets.only(left: 10.px, right: 10.px),
  118. decoration: BoxDecoration(
  119. borderRadius: BorderRadius.circular(20.px),
  120. color: const Color(0xFF2C2C2E)),
  121. child: Row(
  122. children: [
  123. SvgPicture.asset(
  124. 'assets/icons/search.svg',
  125. width: 16.px,
  126. height: 16.px,
  127. ),
  128. SizedBox(
  129. width: 12.px,
  130. ),
  131. Expanded(
  132. child: TextField(
  133. autofocus: true,
  134. cursorColor: kBtnColor,
  135. onChanged: (value) {
  136. setState(() {
  137. keyword = value;
  138. });
  139. },
  140. style: TextStyle(
  141. color: Colors.white,
  142. fontSize: 14.px),
  143. decoration: InputDecoration(
  144. border: InputBorder.none,
  145. hintText: '请输入您的昵称',
  146. hintStyle: TextStyle(
  147. fontSize: 14.px,
  148. color: const Color(0xFF74747A)),
  149. counterText: "",
  150. ),
  151. ))
  152. ],
  153. ),
  154. )),
  155. GestureDetector(
  156. onTap: (){
  157. Navigator.of(context).pop();
  158. },
  159. child: Container(
  160. width: 72.px,
  161. alignment: Alignment.center,
  162. child: Text(
  163. '取消',
  164. style: TextStyle(color: kThemeColor, fontSize: 14.px),
  165. ),
  166. ),
  167. )
  168. ],
  169. ),
  170. )
  171. ],
  172. ),
  173. ])));
  174. }
  175. }