如何在应用程序范围内的函数中调用setState?

问题描述 投票:0回答:1

我有一个应用程序范围的文件,其中包含多个在应用程序中的多个位置使用的函数。在其中一个函数中,我想调用

setState
。这会导致问题,因为该函数只是一个函数声明,不在任何有状态小部件中,但在整个应用程序中使用它。有没有办法让这个工作或者我需要在多个页面上声明相同的函数?

功能


    List<Widget> assembleHeaderWidgets(screenHeight, screenWidth, headers) {
      List<Widget> widgetRows = [];
    
      for (var header in headers) {
        widgetRows.add(
          Container(
            alignment: AlignmentDirectional.center,
            width: screenWidth * header[header.keys.first]['width'],
            height: 60,
            decoration: const BoxDecoration(
              border: Border(
                top: BorderSide(color: Colors.black, width: 2),
                left: BorderSide(color: Colors.black, width: 1),
                bottom: BorderSide(color: Colors.black, width: 2)           
              )
            ),
            child: TextField(
              controller: header[header.keys.first]['controller'],
              enabled: header[header.keys.first]['enabled'],
              textAlign: TextAlign.center,
              style: const TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 15
              ),
              decoration: InputDecoration(
                border: InputBorder.none,
                hintText: header.keys.first,
              ),
              onChanged: (text) {
                //This is where I need to setState
              },
            ),
          )
        );
      }
      return widgetRows;
    }
Widget build(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    double screenHeight = MediaQuery.of(context).size.height;
    if (customersStatic.isEmpty) {
      WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {}));
    }
  
    return Scaffold(
      appBar: AppBar(
        backgroundColor: const Color.fromARGB(255, 230, 36, 36),
        centerTitle: true,
        title: const Text(
          'Customers',
          style: TextStyle(
            color: Colors.white,
            fontSize: 25
          ),
        ),
      ),
      drawer: Drawer(
        child: Column(
          children: funcs.getDrawerItems(context, screenHeight, screenWidth)
        )
      ),
      body: Column(
        children: [
          Row(
            children: funcs.assembleHeaderWidgets(
              screenHeight, 
              screenWidth,
              [
                {'Record ID': {'controller': recordIdCont, 'width': .125, 'enabled': true}},
                {'Customer Name': {'controller': nameCont, 'width': .125, 'enabled': true}},
                {'Address': {'controller': addressCont, 'width': .125, 'enabled': true}},
                {'Address2': {'controller': address2Cont, 'width': .125, 'enabled': true}},
                {'City, State': {'controller': cityStateCont, 'width': .125, 'enabled': true}},
                {'Zip': {'controller': zipCont, 'width': .125, 'enabled': true}},
                {'Zip 4': {'controller': zip4Cont, 'width': .125, 'enabled': true}},
                {'Phone': {'controller': phoneCont, 'width': .125, 'enabled': true}}
              ]
            )
          ),
...
flutter dart setstate
1个回答
0
投票

您可以在公共函数中将

VoidCallback
作为参数传递,并在所有逻辑完成后调用它。像这样:

import 'package:flutter/material.dart';

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

  @override
  State<Demo> createState() => _DemoState();
}

class _DemoState extends State<Demo> {
  void updateWidgetFunction() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () => commonFunction(updateWidgetFunction),
      child: Text(
        'Hello, World!',
        style: Theme.of(context).textTheme.headlineMedium,
      ),
    );
  }
}

void commonFunction(VoidCallback? callBack) {
  ///Your logic here
  callBack?.call();
}
© www.soinside.com 2019 - 2024. All rights reserved.