Flutter - calling setState() from other widget

后端 未结 3 1212
臣服心动
臣服心动 2020-12-17 10:25

Is it possible to call setState() of particular widget (embedded in other widgets) from other widgets onPressed() method so only that

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 10:46

    There are different ways on how to achieve this. The really simple one is with InheritedWidget widget, like that:

    import 'package:flutter/material.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Timer',
          theme: new ThemeData(
            primaryColor: Colors.grey.shade800,
          ),
          home: new MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      MyHomePageState createState() {
        return new MyHomePageState();
      }
    }
    
    class MyHomePageState extends State {
      int _seconds = 1;
    
      @override
      Widget build(BuildContext context) {
        return new MyInheritedWidget(
          secondsToDisplay: _seconds,
          child: Scaffold(
    
            appBar: AppBar(
              title: Text("title"),
            ),
            body: Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                MyTextWidget(), //just update this widget
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    IconButton(
                      icon: Icon(Icons.add_circle),
                      onPressed: _addPressed,
                      iconSize: 150.0,
                    ),
                    IconButton(
                      icon: Icon(Icons.remove_circle),
                      onPressed: () => print("to be implemented"),
                      iconSize: 150.0,
                    ),
                  ],
                )
              ],
            ),
          ),
        );
      }
    
      void _addPressed() {
        setState(() {
          _seconds++;
        });
      }
    }
    
    class MyTextWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final MyInheritedWidget inheritedWidget = MyInheritedWidget.of(context);
        return Text(inheritedWidget.secondsToDisplay.toString(),
          textScaleFactor: 5.0,
        );
      }
    }
    
    class MyInheritedWidget extends InheritedWidget {
      final int secondsToDisplay;
    
      MyInheritedWidget({
        Key key,
        @required this.secondsToDisplay,
        @required Widget child,
      }) : super(key: key, child: child);
    
      static MyInheritedWidget of(BuildContext context) {
        return context.inheritFromWidgetOfExactType(MyInheritedWidget);
      }
    
      @override
      bool updateShouldNotify(MyInheritedWidget oldWidget) =>
          secondsToDisplay != oldWidget.secondsToDisplay;
    }
    

提交回复
热议问题