我需要到添加更多的窗口小部件时onpressed in flutter

dxxyhpgq  于 2023-02-25  发布在  Flutter
关注(0)|答案(2)|浏览(109)

这是我的Flutter启动代码

class MyHomePage extends StatefulWidget {
      const MyHomePage({Key? key, required this.title}) : super(key: key);
    
      final String title;
      @override
      // ignore: library_private_types_in_public_api
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      final formkey = GlobalKey<FormState>();
      File? inchargePic;
      File? support1Pic;
      File? support2Pic;
      File? kmPic;
      bool _isExpanded = false;
      TextEditingController totalKMController = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        final size = MediaQuery.of(context).size;
        return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            title: Text(widget.title),
          ),
          body: SizedBox(
            height: size.height,
            width: size.width,
            child: SingleChildScrollView(
              padding: const EdgeInsets.all(20),
              physics: const BouncingScrollPhysics(),
              child: Column(
                children: [
                  inchargeName(),
                  space(),
                  inchargeImage(),
                  space(),
                  addSupport(),
                  space(),
                  kmAndImagePicWidget(size.height, size.width),
                  space(),
                  elevatedButton(),
                ],
              ),
            ),
          ),
        );
      }

这是我调用按下顶部添加小工具的函数的地方

SizedBox addSupport() {
    return SizedBox(
      child: SingleChildScrollView(
        child: Column(
          children: [
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10))),
              onPressed: () {
                setState(() {
                  _isExpanded = !_isExpanded;
                });
              },
              child: const Text("Add Support"),
            ),
            space(),
            if (_isExpanded) support1()
          ],
        ),
      ),
    );
  }

确切的问题是,我可以在按下时添加support1,但我还需要已添加但不知道如何调用的support2小部件

3htmauhk

3htmauhk1#

If you want to add support widgets one by one, you can modify the _isExpanded variable to keep track of how many support widgets have been added

class _MyHomePageState extends State<MyHomePage> {
  final formkey = GlobalKey<FormState>();
  File? inchargePic;
  List<File?> supportPics = [];
  File? kmPic;
  int _supportCount = 0;
  TextEditingController totalKMController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(widget.title),
      ),
      body: SizedBox(
        height: size.height,
        width: size.width,
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(20),
          physics: const BouncingScrollPhysics(),
          child: Column(
            children: [
              inchargeName(),
              space(),
              inchargeImage(),
              space(),
              addSupport(),
              space(),
              kmAndImagePicWidget(size.height, size.width),
              space(),
              elevatedButton(),
            ],
          ),
        ),
      ),
    );
  }

  SizedBox addSupport() {
    return SizedBox(
      child: SingleChildScrollView(
        child: Column(
          children: [
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10)
                )
              ),
              onPressed: () {
                setState(() {
                  _supportCount++;
                  supportPics.add(null);
                });
              },
              child: const Text("Add Support"),
            ),
            space(),
            for (int i = 0; i < _supportCount; i++) support(i),
          ],
        ),
      ),
    );
  }

  Widget support(int index) {
    return Column(
      children: [
        if (index > 0) space(),
        Row(
          children: [
            Text("Support ${index + 1}:"),
            IconButton(
              onPressed: () {
                setState(() {
                  _supportCount--;
                  supportPics.removeAt(index);
                });
              },
              icon: Icon(Icons.remove),
            ),
          ],
        ),
        space(),
        supportImagePicker(index),
      ],
    );
  }

  Widget supportImagePicker(int index) {
    return ElevatedButton(
      style: ElevatedButton.styleFrom(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10)
        )
      ),
      onPressed: () async {
        final pickedFile =
            await ImagePicker().pickImage(source: ImageSource.gallery);
        setState(() {
          supportPics[index] = File(pickedFile!.path);
        });
      },
      child: Text(supportPics[index] == null
          ? "Select Support ${index + 1} Picture"
          : "Support ${index + 1} Picture Selected"),
    );
  }

  // other functions omitted for brevity
}
9fkzdhlc

9fkzdhlc2#

To add support2 widget when the "Add Support" button is pressed, you can add another condition in the if statement in the addSupport() function to check the value of _isExpanded. Here's the modified addSupport() function with support2 widget added:

SizedBox addSupport() {
  return SizedBox(
    child: SingleChildScrollView(
      child: Column(
        children: [
          ElevatedButton(
            style: ElevatedButton.styleFrom(
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10)
              )
            ),
            onPressed: () {
              setState(() {
                _isExpanded = !_isExpanded;
              });
            },
            child: const Text("Add Support"),
          ),
          space(),
          if (_isExpanded) support1(),
          if (_isExpanded) support2(),
        ],
      ),
    ),
  );
}

both support1 and support2 widgets will be added when _isExpanded is true.

相关问题