From b61e860d4dfe7c7a9ba79837ae4ba4f3a4ef2c0d Mon Sep 17 00:00:00 2001 From: laiiihz Date: Thu, 26 Nov 2020 10:53:21 +0800 Subject: [PATCH] searchTextField add placeholder button --- example/lib/example_text_field.dart | 7 +++ lib/text_field/as_search_text_field.dart | 70 +++++++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/example/lib/example_text_field.dart b/example/lib/example_text_field.dart index 4c19f8b..d6afa4f 100644 --- a/example/lib/example_text_field.dart +++ b/example/lib/example_text_field.dart @@ -18,6 +18,13 @@ class _ExampleTextFiledState extends State { controller: _controller, hintText: '搜索', ), + body: ListView( + children: [ + ASSearchTextField.button( + hintText: '搜索', + ), + ], + ), ); } } diff --git a/lib/text_field/as_search_text_field.dart b/lib/text_field/as_search_text_field.dart index 9386506..326fa6a 100644 --- a/lib/text_field/as_search_text_field.dart +++ b/lib/text_field/as_search_text_field.dart @@ -12,7 +12,25 @@ class ASSearchTextField extends StatefulWidget implements PreferredSizeWidget { this.focusNode, this.onSubmitted, this.margin, - }) : super(key: key); + this.onPressed, + }) : button = false, + super(key: key); + + /// + ASSearchTextField.button({ + Key key, + this.controller, + this.hintText, + this.onChanged, + this.focusNode, + this.onSubmitted, + this.margin, + this.onPressed, + }) : button = true, + super(key: key); + + ///渲染成按钮 + final bool button; ///控制器 final TextEditingController controller; @@ -32,6 +50,8 @@ class ASSearchTextField extends StatefulWidget implements PreferredSizeWidget { ///margin final EdgeInsets margin; + final VoidCallback onPressed; + @override _ASSearchTextFieldState createState() => _ASSearchTextFieldState(); @@ -44,11 +64,56 @@ class _ASSearchTextFieldState extends State { borderRadius: BorderRadius.circular(21.w), borderSide: BorderSide( color: Color(0xFF979797), + width: 1.w, ), ); + _buildButton() { + return Padding( + padding: widget.margin ?? + EdgeInsets.symmetric( + horizontal: 15.w, + vertical: 3.w, + ), + child: MaterialButton( + padding: EdgeInsets.zero, + color: Color(0xFFF8F8F8), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(21.w), + side: BorderSide( + color: Color(0xFF979797), + width: 1.w, + ), + ), + child: Row( + children: [ + Padding( + padding: EdgeInsets.only(left: 13.w, right: 8.w), + child: Icon( + Icons.search, + size: 16.w, + color: Colors.black, + ), + ), + Expanded( + child: Text( + widget.hintText ?? '', + style: TextStyle( + color: kTextSubColor, + fontSize: 14.sp, + ), + ), + ), + ], + ), + onPressed: widget.onPressed, + ), + ); + } + @override Widget build(BuildContext context) { + if (widget.button) return _buildButton(); return Container( height: 42.w, padding: widget.margin ?? @@ -60,10 +125,13 @@ class _ASSearchTextFieldState extends State { controller: widget.controller, onChanged: widget.onChanged, onSubmitted: widget.onSubmitted, + onTap: widget.onPressed, focusNode: widget.focusNode, cursorColor: kPrimaryColor, textInputAction: TextInputAction.search, decoration: InputDecoration( + fillColor: Color(0xFFF8F8F8), + filled: true, hintText: widget.hintText, hintStyle: TextStyle( color: kTextSubColor,