Flutter ---- Key

2020-11-22  本文已影响0人  阿木小丸子

Key

Key 本身是一个抽象类

class GlobalKeyDemo extends StatelessWidget {
 final GlobalKey<_ChildPageState> _globalKey = GlobalKey();

 @override
 Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(),
     body: ChildPage(key: _globalKey),
     floatingActionButton: FloatingActionButton(
       child: Icon(Icons.add),
       onPressed: () {
         _globalKey.currentState.data =
             'old:' +_globalKey.currentState.count.toString();
         _globalKey.currentState.count ++;
         _globalKey.currentState.setState(() { });
       },
     ),
   );
 }
}

class ChildPage extends StatefulWidget {
 ChildPage({Key key}) : super(key: key);
 @override
 _ChildPageState createState() => _ChildPageState();
}

上一篇 下一篇

猜你喜欢

热点阅读