Flutter interface callback callback specific implementation case

Foreword:

Hello everyone, everyone. I haven't updated the article for you for a while. In Android and IOS, we often use interface callbacks to communicate when interface and method calls are made. The writing is more straightforward and we will share how to use flutter today.

Ready to work:

Need to install flutter development environment: everyone can go to see the previous tutorial:
1 Win system flutter development environment installation tutorial: https://www.jianshu.com/p/152447bc8718
2 Mac system flutter development environment installation tutorial: https:/ /www.jianshu.com/p/bad2c35b41e3

Effect picture:

QQ screenshot 20201207114550.png
QQ screenshot 20201207114531.png

Implementation:

We can see from the log that we clicked on the item in the listview to print the subscript and the text displayed in the item in the callback method:

import 'package:flutter/material.dart';
typedef _CallBack = void Function(int selectIndex, String selectStr);
class TextList extends StatefulWidget {
  final List dataArr;
  final _CallBack  callback;
  TextList({Key key, this.dataArr,this.callback }) : super(key: key);
  @override
  _TextListState createState() {
    return _TextListState();
  }
}

class _TextListState extends State<TextList> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar:  AppBar(
       title: Text("接口回调列表 "),
      ),
      body: ListView.builder(
        itemCount: widget.dataArr.length,
        itemBuilder: (BuildContext context, int position){
          return   _itemWidget(context, position);
        },
      ),
    );
  }
  Widget _itemWidget(BuildContext  context, int index){
    return GestureDetector(
      child: Center(
        child:Padding(
        padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
          child: Text(widget.dataArr[index]),
        )
      ),
      onTap: (){
        if(widget.callback!=null){
          widget.callback(index,widget.dataArr[index]);
        }
      },
    );
  }
}

We define the dataArr array and callback method in the TextList class to be instantiated in the TextList and passed in from the constructor

  final List dataArr;
  final _CallBack  callback;

Then define a global CallBack interface attribute:

typedef _CallBack = void Function(int selectIndex, String selectStr);

Called in the ontap method of the listview item click event:

   onTap: (){
        if(widget.callback!=null){
          widget.callback(index,widget.dataArr[index]);
        }
      },

TextList concrete instantiation call:

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return  TextList(
      dataArr: datalist,
      callback: (index, str){
      print("indexe   -----  >   "+index.toString());
      print("str----  >   "+str);
      },
    );
  }

Fake data:

   List  datalist= new List();
  @override
  void initState() {
    super.initState();
    for(var i=0 ; i<20 ;i++){
      datalist.add("第几$i条数据");
    }
  }

Complete homepage code:

import 'package:flutter/material.dart';
import 'text_list.dart';
/**
 *  创建人:xuqing
 *  创建时间:2020年12月7日10:03:31
 *  类是说明:data 接口回调测试类
 *
 */
class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);
  @override
  _HomePageState createState() {
    return _HomePageState();
  }
}
class _HomePageState extends State<HomePage> {
  List  datalist= new List();
  @override
  void initState() {
    super.initState();
    for(var i=0 ; i<20 ;i++){
      datalist.add("第几$i条数据");
    }
  }
  @override
  void dispose() {
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return  TextList(
      dataArr: datalist,
      callback: (index, str){
      print("indexe   -----  >   "+index.toString());
      print("str----  >   "+str);
      },
    );
  }
}

So far, the interface callback of flutter is finished:

Final summary:

The interface callback of flutter is still very similar to java. Only flutter does not use the keyword interface to define and uses the typedef attribute to process it. We need to pay attention to this. This article is just a brief explanation and there are many other more flexible users. I won’t start talking about it. If you are interested, you can study it privately. Finally, I hope that my article can help you solve your problems. I will contribute more useful code to share with you in the future. If you think the article is pretty good, please give attention and star, I thank you here, you can also add my personal QQ/WeChat (1693891473)

project address:

Code Cloud: https://gitee.com/qiuyu123/flutter_callback

QQ exchange group:

92437359.png

Guess you like

Origin blog.csdn.net/xq610928/article/details/110818233