简体   繁体   English

Flutter GetX 多控制器在同一个小部件中不起作用

[英]Flutter GetX multi controller not work in the same widget

CODE:代码:

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    _counter++;
  }

  @override
  Widget build(BuildContext context) {
    TestInt testInt2 = TestInt(100);
    TestInt testInt1 = TestInt(0);

    Get.put<TestInt>(testInt2, tag: "testInt2");
    Get.put<TestInt>(testInt1, tag: "testInt1");
    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: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times: $_counter',
            ),
            GetBuilder<TestInt>(
              init: Get.find<TestInt>(tag: "testInt2"),
              builder: (_) {
                return Text(
                  '${Get.find<TestInt>(tag: "testInt2").number}',
                  style: Theme.of(context).textTheme.headline4,
                );
              },
            ),
            GetBuilder<TestInt>(
              init: Get.find<TestInt>(tag: "testInt1"),
              builder: (_) {
                return Text(
                  '${Get.find<TestInt>(tag: "testInt1").number}',
                  style: Theme.of(context).textTheme.headline4,
                );
              },
            ),
          ],
        ),
      ),
      persistentFooterButtons: [
        FloatingActionButton(
          onPressed: () {
            _incrementCounter();
            Get.find<TestInt>(tag: "testInt2").increment();
          },
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
        FloatingActionButton(
          onPressed: () {
            _incrementCounter();
            Get.find<TestInt>(tag: "testInt1").increment();
          },
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
      ], // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

class TestInt extends GetxController {
  late int number;

  TestInt(number) {
    this.number = number;
  }

  void increment() {
    this.number = number + 1;
    update();
  }
}

Summary:概括:

On the code above, I made 2 floating buttons and 2 GetBuilder to show testInt1's number and testInt2's number separately.在上面的代码中,我制作了 2 个浮动按钮和 2 个 GetBuilder 来分别显示 testInt1 的编号和 testInt2 的编号。

But Only one button works well and the other button doesn't work.但是只有一个按钮可以正常工作,而另一个按钮不起作用。

I think it might works only first initiation with init: Get.find<TestInt>(tag: "testInt2"), and second init: Get.find<TestInt>(tag: "testInt1"), does not works..我认为它可能只适用于init: Get.find<TestInt>(tag: "testInt2"),和第二个init: Get.find<TestInt>(tag: "testInt1"),不起作用..

Question: How to make both works?问题:如何使两者都有效?

Set global:false to your GetBuilder.global:false设置为您的 GetBuilder。

GetBuilder<TestInt>(
              init: Get.find<TestInt>(tag: "testInt2"),
              global: false,
              builder: (_) {
                return Text(
                  '${Get.find<TestInt>(tag: "testInt2").number}',
                  style: Theme.of(context).textTheme.headline4,
                );
              },
            ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM