RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1145536
Accepted
Andrey Khan
Andrey Khan
Asked:2020-06-25 18:58:57 +0000 UTC2020-06-25 18:58:57 +0000 UTC 2020-06-25 18:58:57 +0000 UTC

Flutter 有状态的小部件

  • 772

我不止一次读到不建议使用 statefullWidget。BottomNavigationBar在中切换页面的示例body。没有statefullWidget这个例子怎么办?使用块?还是仅适用于业务逻辑?

class HomeScreenTest extends StatefulWidget {

@override
  _HomeScreenTestState createState() => _HomeScreenTestState();
}

class _HomeScreenTestState extends State<HomeScreenTest> {
  int _currentIndex = 0;
  final pages = [
    Page1(),
    Page2(),
    Page3(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: pages[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        currentIndex: _currentIndex,
        items: [
          BottomNavigationBarItem(
            title: Text('pag1'),
          ),
          BottomNavigationBarItem(
            title: Text('page'),
          ),
          BottomNavigationBarItem(
            title: Text('page3'),
          ),
        ],
      ),
    );
  }
}
flutter
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    MiT
    2020-06-26T01:42:03Z2020-06-26T01:42:03Z

    StatefulWidget确实不能到处使用,但首先让我们弄清楚为什么。

    当我们调用State.setState时,它StatefulWidget​​开始重建。重建时,它会删除树中它下面的元素(嵌套的 chlid),从而导致所有内容都被重建到小部件树的末尾。由于这种重建,isolate被阻塞,其中 UI 工作并且设备的资源被消耗。

    StatefulWidget没有State.setState. _ _ 或者在小部件树的末尾,当它不会强制重建较低的小部件时。


    但是我们不能拒绝State.setState,我们需要动态改变UI,这有什么用呢?

    状态管理- UI 重建优化。有很多,它们都有不同的方法:

    1. Bloc是基于 Stream 的不可变状态,即所谓的反应式接口。
    2. MobX 是一个带有突变的可变状态。
    3. Redux是具有单向数据流的不可变全局状态。
    4. 和别的...

    更改通知器- 通知小部件有关状态更改的通知,适用于少数侦听器。

    Persist Widget - UI 缓存,我们可以从缓存中显示它,而不是重建小部件。


    那么用于什么BottomNavigationBar和类似的小任务呢?

    答案:使用更改通知器(+ Persist Widget,如果它适合任务)。对于此类任务,使用状态管理将是多余的。这是如何完成此操作的示例(提供程序用于方便使用Change Notifier):

    import 'package:flutter/material.dart';
    import 'package:provider/provider.dart';
    
    void main() {
      runApp(
        MaterialApp(
          home: MultiProvider(
            providers: [
              ChangeNotifierProvider(create: (_) => Index()),
            ],
            child: BottomNavigationBarController(),
          ),
        ),
      );
    }
    
    class Index with ChangeNotifier {
      int _currentIndex = 0;
    
      get currentIndex => _currentIndex;
    
      void setIndex(int index) {
        _currentIndex = index;
        notifyListeners();
      }
    }
    
    class BottomNavigationBarController extends StatelessWidget {
      final List<Widget> pages = [
        FirstPage(
          key: PageStorageKey('Page1'),
        ),
        SecondPage(
          key: PageStorageKey('Page2'),
        ),
      ];
    
      final PageStorageBucket bucket = PageStorageBucket();
    
      Widget _bottomNavigationBar(BuildContext context, int selectedIndex) =>
          BottomNavigationBar(
            onTap: (int index) => context.read<Index>().setIndex(index),
            currentIndex: selectedIndex,
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.add),
                title: Text('First Page'),
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.list),
                title: Text('Second Page'),
              ),
            ],
          );
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          bottomNavigationBar:
              _bottomNavigationBar(context, context.watch<Index>().currentIndex),
          body: PageStorage(
            child: pages[context.watch<Index>().currentIndex],
            bucket: bucket,
          ),
        );
      }
    }
    
    class FirstPage extends StatelessWidget {
      const FirstPage({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Text('FirstPage'),
        );
      }
    }
    
    class SecondPage extends StatelessWidget {
      const SecondPage({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Center(
          child: Text('SecondPage'),
        );
      }
    }
    
    • 2

相关问题

  • 1 秒后打印每个列表值

  • 颤振函数调用

  • 如何将代码分配给参数

  • 如何在字符串中放置空格?

  • 更改 Firestore 中的布尔值

  • 显示数据

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5