2)Learning diary for flutter about android

When everything is ready. I  wrote an android  program with flutter

1)Listview is often used in Android ,so first do it .

Android Studio will not report an error if the return value is not written. 

import 'package:flutter/widgets.dart';

abstract class MyBaseAdapter{
  List  data;
  MyBaseAdapter(List data){
    this.data=data;
  }
  getItemList(BuildContext context){
    List<Widget> list=[];
    for(int i=0;i<data.length;i++) {
      list.add(getItem(context, i));
    }
    return list;
  }
  Widget getItem(BuildContext context,int pos);
}
import 'package:flutter/cupertino.dart';
import 'MyBaseAdapter.dart';
getList(BuildContext context,List data) {
  return ListView(itemExtent: 30,children:new MyListAdater(data).getItemList(context));
}

class MyListAdater extends MyBaseAdapter{
  MyListAdater(List data) : super(data);

  @override
  Widget getItem(BuildContext context, int pos) {
    // TODO: implement getItem
    return new Text(data[pos]);
  }
}
class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      // This call to setState tells the Flutter framework that something has
      // changed in this State, which causes it to rerun the build method below
      // so that the display can reflect the updated values. If we changed
      // _counter without calling setState(), then the build method would not be
      // called again, and so nothing would appear to happen.
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body:getList(context,getData()));
  }
  List getData(){
      List data=[];
      for(int i=0;i<10;i++){
        data.add("item_$i");
      }
      return data;
  }
}

2)Next, add layout and click event to the listview

import 'dart:ui';
import 'package:flutter/widgets.dart';

class MyListData {
  String title;
  String context;
  MyListData(this.title, this.context);
}

class LayoutText {
  static TextStyle bigText = new TextStyle(fontSize: 30);
  static TextStyle normalText = new TextStyle(fontSize: 20);
}

Widget getLayout(MyListData v, int pos) {
  return Container(
      height: 100,
      child: Row(children: <Widget>[
        Container(
          child: new GestureDetector(
              child: Image(
                  image: AssetImage("images/ic_launcher.png"),
                  width: 100,
                  height: 100,
                  fit: BoxFit.fill),
              onTap: () {
                print("click$pos" + v.title);
              }),
        ),
        Column(children: <Widget>[
          Text(v.title, style: LayoutText.bigText),
          Text(v.context, style: LayoutText.normalText)
        ])
      ]));
}
import 'package:flutter/widgets.dart';

abstract class MyBaseAdapter<T>{
  List <T> data;
  MyBaseAdapter(this.data);

  getItemList(BuildContext context){
    List<Widget> list=[];
    for(int i=0;i<data.length;i++) {
      list.add(getItem(context, i));
    }
    return list;
  }
  Widget getItem(BuildContext context,int pos);
}
 List getData(){
      List <MyListData>data=[];
      for(int i=0;i<10;i++){
        data.add(new MyListData("item_title$i","item_content$i"));
      }
      return data;
  }

 

發佈了26 篇原創文章 · 獲贊 0 · 訪問量 8035
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章