-
TabBarView PageView에서 위젯 리빌드 방지flutter 2021. 5. 3. 19:10
PageView등에서 children 위젯을 여러개 가질경우, 네비게이션바에서 그페이지가 아닌
다른 페이지를 들어가도, 전체 위젯이 계속 빌드됨..
불필요한 rebuild를 막기위한 방법..
stackoverflow.com/questions/57704265/how-to-prevent-rebuild-statelesswidget-children-of-pageview
How to prevent rebuild StatelessWidget children of PageView
I've create simple PageView app to test multiple pages. import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(
stackoverflow.com
const keyword
- Make your widgets accept to be const:@override Widget build(BuildContext context) { debugPrint("FirstPage.build"); return Container( child: Center( child: Text("First Page"), ), ); }}
- class FirstPage extends StatelessWidget { const FirstPage({Key key}) : super(key: key);
- and call it with const keyword:return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: PageView( children: <Widget>[ const firstPage(), const secondPage(), ], ), );
2. AutomaticKeepAliveClientMixin
- Convert your StatelessWidget to StatefullWidget.
class FirstPage extends StatefulWidget { FirstPage({Key key}) : super(key: key); @override _FirstPageState createState() => _FirstPageState(); } class _FirstPageState extends State<FirstPage> { @override Widget build(BuildContext context) { debugPrint("FirstPage.build"); return Container( child: Center( child: Text("First Page"), ), ); } }
- Extends AutomaticKeepAliveClientMixin on StatefullWidget created State.
class _FirstPageState extends State<FirstPage> with AutomaticKeepAliveClientMixin {
- Call super on the build method.
@override Widget build(BuildContext context) { super.build(context); debugPrint("FirstPage.build"); return Container( child: Center( child: Text("First Page"), ), ); }
- Override wantKeepAlive getter with true returned value.
@override bool get wantKeepAlive => true;
'flutter' 카테고리의 다른 글
bloc패턴 -rxdart를 활용한 약식 (0) 2021.07.03 flutter bloc 패턴, rxdart.. (0) 2021.07.03 null safety (0) 2021.05.02 플러터 라이프 사이클 (0) 2021.04.21 runtransaction 트랜잭션 처리 (0) 2021.04.21