溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

flutter如何傳遞值到任意widget

發(fā)布時間:2021-08-10 09:27:10 來源:億速云 閱讀:136 作者:小新 欄目:移動開發(fā)

這篇文章主要介紹flutter如何傳遞值到任意widget,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

如果我們有這樣一個應(yīng)用場景:

WidgetA執(zhí)行點擊之后將數(shù)據(jù)通過widgetB傳遞到其下的widgetC。

通常可以通過設(shè)置構(gòu)造函數(shù),傳遞對應(yīng)參數(shù)到制定的widget樹中,如下面代碼所描述:

表示需要將widgetA中的點擊改變內(nèi)容傳遞到widgetB中的widgetC中展示;

需要通過設(shè)置widgetB的構(gòu)造函數(shù),接收對應(yīng)參數(shù),再傳遞給widgetC展示;

class Inheritedwidget extends StatefulWidget {
 @override
 _InheritedWidgetState createState() => _InheritedWidgetState();
}
class _InheritedWidgetState extends State<Inheritedwidget> {
 int count=0;
 @override
 void initState() {
  // TODO: implement initState
  super.initState();
 }
 @override
 Widget build(BuildContext context) {
  print(count);
  return Scaffold(
   appBar: AppBar(title: Text("inherited widget"),),body: Container(
   child: Center(
    child: Column(
     children: <Widget>[
      Text("class0"),
      class1(count),
     ],
    ),
   ),
  ),
   floatingActionButton: FloatingActionButton(onPressed: (){
    return addCount();
   },child: Text("add"),),
  );
 }
 void addCount() {
  setState(() {
   count=1+count;
  });
 }
}

WidgetB:

class class1 extends StatelessWidget {
 int count;
 class1(this.count);
 @override
 Widget build(BuildContext context) {
  return Container(
   child: Column(
     children: <Widget>[
      Text("class1"),
      class2(count),
     ],
   ),
  );
 }
}

widgetC:

class class2 extends StatelessWidget {
 int count;
 class2(this.count);

 @override
 Widget build(BuildContext context) {
  return Container(
   child: Center(
    child: Text("$count"),
   ),
  );
 }
}

以上方法當(dāng)然可以實現(xiàn)需要的效果,但是當(dāng)有多層的widget嵌套關(guān)系的時候代碼閱讀性降低,可以通過以下方法傳遞值到指定的widget中;

通過類似于Android中的contentProvider提供一個中間類,將需要傳遞的數(shù)據(jù)通過中間類傳遞到制定的widget中。

中間類:

//countProvider類 提供count屬性和child屬性 用于與原widget相關(guān)聯(lián),
class CountProvider extends InheritedWidget{
 final int count;
 final Widget child;
 //構(gòu)造方法
 CountProvider({this.count, this.child}):super(child:child);
 //提供方法獲取到countprovider類對象
static CountProvider of(BuildContext context){
 return context.inheritFromWidgetOfExactType(CountProvider);
}
 @override
 bool updateShouldNotify(InheritedWidget oldWidget) {
  // TODO: implement updateShouldNotify
  return false;
 }
}

通過counterprovider包裹需要展示的widget并傳入需要改變的值;

class Inheritedwidget extends StatefulWidget {
 @override
 _InheritedWidgetState createState() => _InheritedWidgetState();
}
class _InheritedWidgetState extends State<Inheritedwidget> {
 int count=0;
 @override
 Widget build(BuildContext context) {
  print(count);
  return CountProvider(
   count:count,
   child: Scaffold(
    backgroundColor: Colors.blue,
    appBar: AppBar(title: Text("inherited widget"),),body: Container(
    child: Center(
     child: Column(
      children: <Widget>[
       Text("class0"),
       class1(),
      ],
     ),
    ),
   ),
    floatingActionButton: FloatingActionButton(onPressed: (){
     return addCount();
    },child: Text("add"),),
   ),
  );
 }
 void addCount() {
  setState(() {
   count=1+count;
  });
 }
}

使用中間類提供的數(shù)據(jù)執(zhí)行更新對應(yīng)widget。

class class2 extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
  int count = CountProvider.of(context).count;
  return Container(
   child: Center(
    child: Text("$count"),
   ),
  );
 }
}

通過以上方法即可在不同widget中傳遞需要改變的值。

以上是“flutter如何傳遞值到任意widget”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI