Preface:
I haven’t given you an article for some time. On Android and IOS, we use interface callbacks to communicate with each other when making interface and method calls. Today we will share how to use Flutter.
Preparations:
The development environment for Flutter needs to be installed: check out the previous tutorial:
Win System Flutter development environment installation tutorial:
www.jianshu.com/p/152447bc8…
Install the Flutter development environment for MAC system
www.jianshu.com/p/bad2c35b4…
Effect:
Concrete implementation:
We can see from the log that when we click on the item in the listView, we print out the subscript and the text display 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(" Interface callback list "),), 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]); }}); }}Copy the code
We define the dataArr array in the TextList class and the callback method to be passed in from the constructor of the TextList instantiation
final List dataArr;
final _CallBack callback;
Copy the code
Then define a global CallBack interface attribute:
typedef _CallBack = void Function(int selectIndex, String selectStr);
Copy the code
Call from the listView item click event ontap method:
onTap: (){
if(widget.callback!=null){
widget.callback(index,widget.dataArr[index]);
}
},
Copy the code
TextList instantiation call:
@override Widget build(BuildContext context) { // TODO: implement build return TextList( dataArr: datalist, callback: (index, str){ print("indexe ----- > "+index.toString()); print("str---- > "+str); }); }Copy the code
False data:
List datalist= new List(); @override void initState() { super.initState(); for(var i=0 ; i<20 ; I ++){datalist. Add (" datalist "); }}Copy the code
Homepage code:
import 'package:flutter/material.dart'; import 'text_list.dart'; /** ** Created by Xuqing ** * */ 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 (" datalist "); } } @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); }); }}Copy the code
The interface callback to this flutter is complete:
Conclusion:
The interface callback of a flutter is very similar to Java except that a flutter does not use the keyword “interface” but instead uses a typedef property to handle it and we need to be aware that, This article is just a simple explanation of writing and there are many other users who are more flexible. I will not expand on it. If you are interested, you can study privately. If you think the article is good, please pay attention to it and star. Thank you and you can also add my personal QQ/ wechat (1693891473).
Project Address:
Yards cloud: gitee.com/qiuyu123/fl…