【问题标题】:How to implement user tagging in flutter?如何在flutter中实现用户标记?
【发布时间】:2019-08-07 22:47:22
【问题描述】:

我正在构建的应用程序有 cmets 和帖子,我想知道如何在 cmets 和帖子中实现类似于 Instagram 的标记?我认为我上次检查时没有任何软件包。我会像评论和帖子标记中的搜索栏一样实现它吗?但是我不能为此使用搜索代理,因为它会将我带到另一个屏幕,即搜索屏幕,我希望它类似于 Facebook 和 Instagram 的搜索。有什么想法吗?

【问题讨论】:

    标签: flutter dart tagging


    【解决方案1】:

    根据您的要求修改它!

    TextEditingController ctrl;
      List<String> users = ['Naveen', 'Ram', 'Satish', 'Some Other'], 
      words = [];
      String str = '';
      List<String> coments=[];
    
      @override
      void initState() {
        super.initState();
        ctrl = TextEditingController();
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
            color: Colors.white,
            padding: EdgeInsets.all(20),
            child: Column(mainAxisSize: MainAxisSize.min, children: [
              TextField(
                  controller: ctrl,
                  decoration: InputDecoration(
                    hintText: 'Comment',
                    hintStyle: TextStyle(color: Colors.black),
                    suffixIcon: IconButton(
                        icon: Icon(Icons.send, color: Colors.blue),
                        onPressed: () {
                          if(ctrl.text.isNotEmpty)
                            setState((){
                              coments.add(ctrl.text);
                            });
                        }),
                  ),
                  style: TextStyle(
                    color: Colors.black,
                  ),
                  onChanged: (val) {
                    setState(() {
                      words = val.split(' ');
                      str = words.length > 0 &&
                              words[words.length - 1].startsWith('@')
                          ? words[words.length - 1]
                          : '';
                    });
                  }),
              str.length > 1
                  ? ListView(
                      shrinkWrap: true,
                      children: users.map((s){
                        if(('@' + s).contains(str))
                        return 
                          ListTile(
                          title:Text(s,style: TextStyle(color: Colors.black),),
                        onTap:(){
                          String tmp = str.substring(1,str.length);
                          setState((){
                            str ='';
                            ctrl.text += s.substring(s.indexOf(tmp)+tmp.length,s.length).replaceAll(' ','_');
                          });
                        });
                        else return SizedBox();
                      }).toList()
                  ):SizedBox(),
              SizedBox(height:25),
              coments.length>0 ?
              ListView.builder(
                shrinkWrap:true,
              itemCount:coments.length,
              itemBuilder:(con,ind){
              return Text.rich(
                TextSpan(
                text:'',
                children:coments[ind].split(' ').map((w){
                  return w.startsWith('@')&&w.length>1 ?
                    TextSpan(
                  text:' '+w,
                    style: TextStyle(color: Colors.blue),
                    recognizer:new TapGestureRecognizer()..onTap=()=>showProfile(w),
                  ): TextSpan(text:' '+w,style: TextStyle(color: Colors.black));
                }).toList()
                ),
              );
                },
                ):SizedBox()
            ]));
      }
      showProfile(String s){
        showDialog(
        context:context,
        builder:(con)=>
        AlertDialog(
        title:Text('Profile of $s'),
        content:Text('Show the user profile !')
        ));
      }
    

    【讨论】:

    • 你能帮我解决这个问题吗? stackoverflow.com/questions/62201831/…
    • 这太棒了!感谢大佬的分享,在关键时刻帮了我很多!你应该为此创建一个包。
    • 谢谢!这是一个小而简单的代码,我认为这个包没有帮助!
    猜你喜欢
    • 2019-12-21
    • 2021-04-02
    • 2023-04-03
    • 2020-07-19
    • 2019-03-19
    • 2017-11-22
    • 2020-06-25
    • 2020-06-04
    • 2021-05-03
    相关资源
    最近更新 更多