| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import 'package:flutter/material.dart';
- import 'package:twong/router/index.dart';
- import 'package:twong/config/style.dart';
- import 'package:twong/utils/image_utils.dart';
- import 'package:twong/utils/index.dart';
- class SearchBar extends StatefulWidget implements PreferredSizeWidget {
- final Widget leading;
- final Size _size = DStyle.appBarHeight;
- final String search;
- final Function doSearch;
- final bool autoFocus;
- final bool enable;
- final Color color;
- final double elevation;
- final List<Widget> actions;
- SearchBar (this.leading, {Key key, this.actions, this.search, this.doSearch, this.autoFocus = false,
- this.enable = false, this.color = DColors.Main, this.elevation = 3 }): super(key: key);
- @override
- State<StatefulWidget> createState() {
- return SearchBarState();
- }
- @override
- Size get preferredSize => _size;
- }
- class SearchBarState extends State<SearchBar> {
- List<Widget> actions = new List<Widget>();
- onChange (String text) {
- if (text.length < 1) {
- setState(() {
- actions = <Widget>[
- getAction("取消", (){ Navigator.pop(context); }),
- ];
- });
- } else {
- setState(() {
- actions = <Widget>[
- getAction("搜索", (){ widget.doSearch(text); }),
- ];
- });
- }
- }
- Widget getAction(String text, Function onTap) {
- return GestureDetector(
- onTap: onTap,
- child: Container(
- width: 60.px,
- child: Center(
- child: Text(text, style: TextStyle(color: Colors.white, fontWeight: FontWeight.w300), textAlign: TextAlign.center),
- ),
- )
- );
- }
- List<Widget> getActions() {
- if (widget.actions != null) actions = widget.actions;
- if(actions.length > 0) return actions;
- if(widget.enable) {
- return <Widget>[getAction("取消", (){ Navigator.pop(context); })];
- } else {
- return null;
- }
- }
- @override
- Widget build(BuildContext context) {
- return PreferredSize(
- preferredSize: widget.preferredSize,
- child: AppBar(
- titleSpacing: 0.0,
- elevation: widget.elevation,
- backgroundColor: widget.color,
- automaticallyImplyLeading: false,
- leading: widget.leading,
- title: GestureDetector(
- onTap: () {
- if (widget.enable) return;
- Navigator.pushNamed(context, RouteNames.search);
- },
- child: Container(
- height: 30,
- margin: EdgeInsets.only(right: 10.px, left: 10.px),
- child: TextField(
- onChanged: onChange,
- style: TextStyle(color: Color.fromARGB(212, 255, 255, 255), fontSize: 14.px, fontWeight: FontWeight.w300),
- enabled: widget.enable,
- autofocus: widget.autoFocus,
- onSubmitted: widget.doSearch,
- textInputAction: TextInputAction.search,
- decoration: InputDecoration(
- hintText: "美妆 护肤 面膜",
- hintStyle: TextStyle(color: Color.fromARGB(212, 255, 255, 255), fontSize: 14.px, fontWeight: FontWeight.w300),
- prefixIcon: Icon(IconFonts.search, color: Color.fromARGB(212, 255, 255, 255), size: 15),
- contentPadding: const EdgeInsets.symmetric(vertical: 4.0),
- border: OutlineInputBorder(
- borderSide: BorderSide.none,
- borderRadius: BorderRadius.circular(3)
- ),
- ),
- ),
- decoration: BoxDecoration(
- color: Color.fromARGB(90, 255, 255, 255),
- borderRadius: BorderRadius.circular(3),
- ),
- ),
- ),
- actions: getActions(),
- ),
- );
- }
- }
|