ListView

作者: xieyinghao | 来源:发表于2023-06-05 07:26 被阅读0次
    import 'dart:ffi';
    
    import 'package:flutter/material.dart';
    import 'package:testdemo/models/cat.dart';
    
    import '../view/cell.dart';
    
    class TableViewPage extends StatefulWidget {
      const TableViewPage({super.key, required this.title, this.name});
    
      final String title;
      final String? name;
    
      @override
      State<TableViewPage> createState() => _TableViewPageState();
    }
    
    class _TableViewPageState extends State<TableViewPage> {
      int _counter = 0;
      final List<CatModel> _models = [];
    
      @override
      void initState() {
        super.initState();
    
        CatModel model = CatModel(name: 'hahaha', title: 'yes');
    
        _models.add(model);
        _models.add(model);
        _models.add(model);
    
        setState(() {});
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Container(
            child: ListView.builder(
              itemCount: _models.length,
              itemBuilder: (context, index) => CustomCellPage(
                title: _models[index].name ?? "==",
                model: _models[index],
                onCallBack: (index) {
                  print("====index=$index");
                },
                // title: "啊哈哈",
              ),
            ),
          ),
        );
      }
    }
    
    

    模型

    class CatModel {
      String? name;
      String? title;
      CatModel({this.name, this.title});
    }
    
    

    cell

    import 'package:flutter/material.dart';
    
    import '../models/cat.dart';
    
    class CustomCellPage extends StatefulWidget {
      const CustomCellPage(
          {super.key, required this.title, this.model, this.onCallBack});
      final CatModel? model;
      final String title;
      final Function(int)? onCallBack;
    
      @override
      State<CustomCellPage> createState() => _customCellState();
    }
    
    class _customCellState extends State<CustomCellPage> {
      @override
      Widget build(BuildContext context) {
        return GestureDetector(
          onTap: () {
            if (widget.onCallBack != null) {
              widget.onCallBack!(0);
            }
          },
          child: Container(
            // width: double.infinity, //无尽
            // height: double.infinity,
            height: 200,
            width: 200,
            color: Colors.yellow,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(widget.model?.title ?? '=='),
                Text(
                  widget.model?.name ?? '--',
                  style: TextStyle(fontSize: 30, color: Colors.orange),
                ),
                // Image.network(
                //   "https://s3.ax1x.com/2020/12/15/rKiXxs.png",
                //   width: 160,
                //   height: 160,
                // ),
                // Image.asset(
                //   'images/tupian.png',
                //   width: 50,
                //   height: 50,
                // ),
                Image.network(
                    'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533368711441&di=9517daea3b4fc02d74b27a1f0e2d11f8&imgtype=0&src=http%3A%2F%2Fimage13-c.poco.cn%2Fmypoco%2Fmyphoto%2F20121102%2F22%2F66582707201211022200252633924731300_000_640.jpg'),
                TextField(
                  decoration: InputDecoration(
                    hintText: "请输入文本",
                    border: OutlineInputBorder(),
                    fillColor: Colors.red,
                    filled: true,
                    isCollapsed: true,
                  ),
                  textAlign: TextAlign.start,
                  onChanged: (value) => {
                    print(value),
                  },
                )
              ],
            ),
          ),
        );
      }
    }
    
    

    相关文章

      网友评论

          本文标题:ListView

          本文链接:https://www.haomeiwen.com/subject/ksqvedtx.html