第一次点击反应迟钝

时间:2019-05-05 14:41:29

标签: flutter

我有一个示例代码,其中显示了按钮FAB单击的计数器。重新启动应用程序后,第一次单击的反应比平常更长。大约为0,7-1秒,但此后一切正常,可以在0,3-0,5秒内正常工作,这是什么原因或如何更改它。 这几乎是一个基本示例。我不认为问题出在代码上

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: 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> {

  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
      print(_counter);
    });
  }

  void _decrementCounter() {
    setState(() {
      _counter--;
    });
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new CountTarget(
              count: _counter,
              child: Targets(),
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: Wrap(
        //mainAxisAlignment: MainAxisAlignment.end,
        direction: Axis.vertical,
        spacing: 10.0,
        children: <Widget>[
          FloatingActionButton(
            onPressed: _decrementCounter,
            tooltip: 'Decrement',
            child: Icon(Icons.remove),
          ),
          FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),

        ],
      )
    );
  }
}

----- targets.dart

import 'package:flutter/material.dart';

class Targets extends StatefulWidget {

  Targets();

  @override
  createState() => new MyTargetsState();
}

class MyTargetsState extends State<Targets> {

  MyTargetsState();

  @override
  Widget build(BuildContext context) {

    final CountTarget inheritedWidget = CountTarget.of(context);
    return Wrap(
        direction: Axis.horizontal,
        spacing: 8.0,
        children: _getList(inheritedWidget.count)
    );
  }

  List<Widget> _getList(int count){
    return List<Widget>.generate(5, (i) => ((count - i) > 0)
        ?Icon(Icons.brightness_1, size: 18.0, color: Colors.blue)
        :Icon(Icons.radio_button_unchecked, size: 18.0, color: Colors.blue)
    );
  }
}

class CountTarget extends InheritedWidget {
  CountTarget({
    Key key,
    @required Widget child,
    this.count,
  }): super(key: key, child: child);

  final count;

  static CountTarget of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(CountTarget);
  }

  @override
  bool updateShouldNotify(CountTarget oldWidget) => count != oldWidget.count;
}

谢谢。

0 个答案:

没有答案