首页 > 解决方案 > Flutter - 屏幕焦点在某个小部件上

问题描述

我需要帮助来执行以下操作:当我按下 List 1 时,屏幕聚焦在 List 1 上;我需要相同的其余选项

这是示例的代码: 代码

这种行为已经存在于网页中,但我在移动应用程序级别没有发现同样的行为。谢谢

标签: flutterdart

解决方案


这是一个类似的小代码片段,可以帮助您实现所需的结果。

通过单击 fab 图标,它将向下滚动到 ListView 中的第 35 项。

class MyHomePage extends StatelessWidget {
  final _scrollController = ScrollController();
  final _cardHeight = 200.0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      floatingActionButton: FloatingActionButton(
        backgroundColor: Colors.orange,
        onPressed: () => _animateToIndex(35),
        child: Icon(Icons.add),
      ),
      body: ListView.builder(
        controller: _scrollController,
        itemCount: 100,
        itemBuilder: (_, i) => Container(
          height: _cardHeight,
          child: Card(
            color: Colors.lightBlue,
            child: Center(
              child: Text("Scroll Item $i", style: TextStyle(fontSize: 28.0),),
            ),
          ),
        ),
      ),
    );
  }

  _animateToIndex(index) {
    _scrollController.animateTo(_cardHeight * index,
        duration: Duration(seconds: 1), curve: Curves.fastOutSlowIn);
  }
}

推荐阅读