詳解Flutter中StatefulBuilder組件的使用
本文是關(guān)于 Flutter 中的 StatefulBuilder 小部件。我將介紹小部件的基礎(chǔ)知識,然后檢查一個在實際中使用它的完整示例。、StatefulBuilder 小部件可以在這些區(qū)域的狀態(tài)發(fā)生變化時僅重建某些小區(qū)域而無需付出太多努力。這提高了應(yīng)用程序的性能。
StatefulBuilder({ Key? key, required StatefulWidgetBuilder builder r})
builder 函數(shù)有兩個參數(shù):context和一個用于在狀態(tài)改變時觸發(fā)重建的函數(shù):
builder: (context, setInnerState) => SomeWidget(/* ...*/)
另一個注意事項是,您應(yīng)該將保持狀態(tài)的變量保留在構(gòu)建器函數(shù)之外。為了更清楚,請參閱下面的完整示例。
例子
預(yù)覽
我們要構(gòu)建的示例應(yīng)用程序是一種計數(shù)器應(yīng)用程序。它包含一個按鈕和一個位于紅色圓圈內(nèi)的文本小部件。每按一次按鈕,文本小部件中的數(shù)字就會增加一個單位。我們將只使用StatefulBuilder小部件來實現(xiàn)這一點。你不會看到任何StatefulWidget。
以下是它的工作原理:
注意: 如果您使用的是 Safari,此演示視頻可能無法正常播放或根本無法播放。請改用 Chrome、Edge、Firefox 或其他網(wǎng)絡(luò)瀏覽器。
編碼
帶有解釋的完整代碼:
// main.dart import 'package:flutter/material.dart'; ? void main() { runApp(const MyApp()); } ? class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); ? @override Widget build(BuildContext context) { return MaterialApp( // Remove the debug banner debugShowCheckedModeBanner: false, title: '大前端之旅', theme: ThemeData( primarySwatch: Colors.amber, ), home: const HomePage(), ); } } ? // StatelessWidget is used here class HomePage extends StatelessWidget { const HomePage({Key? key}) : super(key: key); ? @override Widget build(BuildContext context) { // This is the number shown in the red circle // It represents state and stays outside the builder function int count = 0; return Scaffold( appBar: AppBar(title: const Text('大前端之旅')), body: Padding( padding: const EdgeInsets.all(30), child: Center( // Implement StatefulBuilder child: StatefulBuilder( builder: (context, setInnerState) => Column( children: [ Container( width: 300, height: 300, decoration: const BoxDecoration( color: Colors.red, shape: BoxShape.circle), child: Center( // Display the number child: Text( count.toString(), style: const TextStyle(fontSize: 80, color: Colors.white), ), ), ), const SizedBox( height: 20, ), // This button is used to crease the number ElevatedButton.icon( onPressed: () { // Call setInnerState function setInnerState(() => count++); }, icon: const Icon(Icons.add), label: const Text('Increase By One')), ], ), ), ), ), ); } }
結(jié)論
您已經(jīng)了解了有關(guān) StatefulBuilder 小部件的幾乎所有內(nèi)容。這在 Flutter 中并不是不可替代的東西,但在實現(xiàn)部分應(yīng)用程序時會給你更多的選擇。
到此這篇關(guān)于詳解Flutter中StatefulBuilder組件的使用的文章就介紹到這了,更多相關(guān)Flutter StatefulBuilder組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在當(dāng)前Activity之上創(chuàng)建懸浮view之WindowManager懸浮窗效果
這篇文章主要介紹了在當(dāng)前Activity之上創(chuàng)建懸浮view之WindowManager懸浮窗效果的相關(guān)資料,需要的朋友可以參考下2016-01-01Android中分析Jetpack?Compose動畫內(nèi)部的實現(xiàn)原理
這篇文章主要介紹了Android中分析Jetpack?Compose動畫內(nèi)部的實現(xiàn)原理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-09-09解決android Listview的item中最外層Margin失效的問題
下面小編就為大家?guī)硪黄鉀Qandroid Listview的item中最外層Margin失效的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Android 優(yōu)化Handler防止內(nèi)存泄露
這篇文章主要介紹了Android 優(yōu)化Handler防止內(nèi)存泄露的相關(guān)資料,這里提供實例幫助大家理解掌握這樣的內(nèi)容,需要的朋友可以參考下2017-09-09Android編程之利用服務(wù)實現(xiàn)電話監(jiān)聽的方法
這篇文章主要介紹了Android編程之利用服務(wù)實現(xiàn)電話監(jiān)聽的方法,較為詳細(xì)的分析了Android基于服務(wù)實現(xiàn)針對電話監(jiān)聽的具體步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2015-11-11Android實現(xiàn)SQLite添加、更新及刪除行的方法
這篇文章主要介紹了Android實現(xiàn)SQLite添加、更新及刪除行的方法,涉及Android基于SQLiteDatabase類操作SQLite數(shù)據(jù)庫的基本技巧,需要的朋友可以參考下2016-08-08Android學(xué)習(xí)之Span的使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android中各種Span類的使用方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-06-06android開發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例
本篇文章主要介紹了android開發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01