Flutter中向Widget子组件传参数(多个参数)

以下是传递参数的示例

import 'package:flutter/material.dart';

class InspectList extends StatefulWidget {
  const InspectList({super.key});

  @override
  State<StatefulWidget> createState() => _InspectListState();
}

class _InspectListState extends State<InspectList>
    with SingleTickerProviderStateMixin {
  //  加载事件
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('title'),
          leading: BackButton(
            onPressed: () {
              Navigator.pop(context);
            },
          ),
          elevation: 2,
          centerTitle: true,
        ),
        body:
            Container(color: Colors.white, child: UnhandBlock("param1", "88")),
      ),
    );
  }
}

class UnhandBlock extends StatefulWidget {
  final String item;
  final String item2;

  UnhandBlock(this.item, this.item2);

  @override
  _UnhandState createState() => _UnhandState();
}

class _UnhandState extends State<UnhandBlock> {
  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Text(widget.item),
        Text(widget.item2),
      ],
    );
  }
}

其中主要是在这一部分

 

posted @ 2024-05-10 11:49  何以平天下  阅读(84)  评论(0编辑  收藏  举报