只需重建一个文本小部件。但仍然占用与重建整个屏幕相同的资源

问题描述 投票:0回答:1

我刚刚使用provider:4.0.5进行了测试,将textview拆分为其他类,并使用consumer为文本重建提供状态,但也许所有小部件也都已重建。我的代码:

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(MyApp());
}

class MyState with ChangeNotifier {
  int counter = 0;

  void incre() {
    counter++;
    notifyListeners();
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: ChangeNotifierProvider(
          create: (_) => MyState(),
          child: MyHomePage(title: 'Flutter Demo Home Page')),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

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

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    print('build');
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Opacity(
              opacity: 0.5,
              child: Opacity(
                opacity: 0.5,
                child: Container(
                  decoration: BoxDecoration(color: Colors.red, boxShadow: [
                    BoxShadow(
                        color: Colors.yellow,
                        offset: Offset(-5, -5),
                        blurRadius: 25,
                        spreadRadius: 25)
                  ]),
                  child: Center(
                    child: Text(
                      'Hello World',
                      style: Theme.of(context).textTheme.headline4,
                    ),
                  ),
                ),
              ),
            ),
            Opacity(
              opacity: 0.5,
              child: Opacity(
                opacity: 0.5,
                child: Container(
                  decoration: BoxDecoration(color: Colors.red, boxShadow: [
                    BoxShadow(
                        color: Colors.yellow,
                        offset: Offset(-5, -5),
                        blurRadius: 25,
                        spreadRadius: 25)
                  ]),
                  child: Center(
                    child: Text(
                      'Hello World',
                      style: Theme.of(context).textTheme.headline4,
                    ),
                  ),
                ),
              ),
            ),
            Opacity(
              opacity: 0.5,
              child: Opacity(
                opacity: 0.5,
                child: Container(
                  decoration: BoxDecoration(color: Colors.red, boxShadow: [
                    BoxShadow(
                        color: Colors.yellow,
                        offset: Offset(-5, -5),
                        blurRadius: 25,
                        spreadRadius: 25)
                  ]),
                  child: Center(
                    child: Text(
                      'Hello World',
                      style: Theme.of(context).textTheme.headline4,
                    ),
                  ),
                ),
              ),
            ),
            Opacity(
              opacity: 0.5,
              child: Opacity(
                opacity: 0.5,
                child: Container(
                  decoration: BoxDecoration(color: Colors.red, boxShadow: [
                    BoxShadow(
                        color: Colors.yellow,
                        offset: Offset(-5, -5),
                        blurRadius: 25,
                        spreadRadius: 25)
                  ]),
                  child: Center(
                    child: Text(
                      'Hello World',
                      style: Theme.of(context).textTheme.headline4,
                    ),
                  ),
                ),
              ),
            ),
            SizedBox(
              height: 35,
            ),
            MyText(),
            RaisedButton(
              onPressed: Provider.of<MyState>(context, listen: false).incre,
//              onTap: () {
//                setState(() {});
//              },
              child: Text('click'),
            )
          ],
        ),
      ),
    );
  }
}

class MyText extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<MyState>(
      builder: (_, state, __) {
        print('buildText2');
        return Text(
          'You have pushed the button this many times: ${state.counter}',
        );
      },
    );
  }
}

这是我仅更新和重建文本时的屏幕,但这非常昂贵。“模拟器屏幕快照

这是我扑扑的医生-v

[✓] Flutter(在Mac OS X 10.15.4 19E287上为Channel dev,v1.18.0-6.0.pre,语言环境为en-VN)•位于/ Users / tbm / dev / flutter_sdk / flutter的Flutter 1.18.0-6.0.pre版•框架修订版84c84fb249(6天前),2020-04-20 21:35:01 -0400•引擎版本2bde4f0ae4•Dart版本2.9.0(内部版本2.9.0-1.0.dev a12c36dd97)

flutter flutter-layout
1个回答
0
投票

避免不必要的重建可以优化CPU时间。您的问题是渲染时间,该时间不受您所做的优化的影响。

您的问题是由于您的用户界面过于复杂而无法绘制。

原因之一是:Opacity非常昂贵,而您使用了[[很多。

代替做:

Opacity( opacity: .5 child: Opacity( opacity: .5 child: ... ), ),

第一步是将两个Opacity合并为一个:

Opacity( opacity: .5 * .5 child: ... ),

然后,为进一步优化,而不是:

[ Opacity( opacity: .5 * .5 child: A(), ), Opacity( opacity: .5 * .5 child: B(), ), Something(), ]

您可能有:

[ Opacity( opacity: .5 * .5, child: Column( mainAxisSize: MainAxisSize.min, children: [ A(), B(), ], ), ), Something(), ]

© www.soinside.com 2019 - 2024. All rights reserved.