简体   繁体   English

如何滚动到不在 Flutter 中的 ListView 中的元素

[英]How to scroll to an element who is not in a ListView in Flutter

I would like to scroll to a particular row (containing the day of week name), but these rows are separated by differents widgets, so I can't put them in a ListView and use an index to scroll, here is a part of my code where you can see the row for monday and the row for tuesday separated by 3 widgets:我想滚动到特定的行(包含星期几的名称),但是这些行由不同的小部件分隔,所以我不能把它们放在 ListView 中并使用索引来滚动,这是我的一部分您可以在其中看到由 3 个小部件分隔的周一行和周二行的代码:

Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                buildHorizontalSpace(10.0),
                                Text(
                                  "Monday :",
                                  style: BeoStyles.whiteSubtitle,
                                )
                              ]
                            ),
                            buildVerticalSpace(10.0),
                            buildDayTasks(1),
                            buildVerticalSpace(10.0),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                buildHorizontalSpace(10.0),
                                Text(
                                  "Tuesday :",
                                  style: BeoStyles.whiteSubtitle,
                                )
                              ]
                            ),

If you have an idea on how to do that you are more than welcome !如果您对如何做到这一点有任何想法,我们非常欢迎!

Using Scrollable.ensureVisible as described in the other SO post , I managed to get it working in an example app.使用Scrollable.ensureVisible另一篇 SO 帖子中所述,我设法让它在示例应用程序中工作。 Let me know if it works for you.请让我知道这对你有没有用。

import 'dart:math';

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  // Seven keys is manageable
  List<GlobalKey> dayKeys = [
    GlobalKey(),
    GlobalKey(),
    GlobalKey(),
    GlobalKey(),
    GlobalKey(),
    GlobalKey(),
    GlobalKey(),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Column(
          // days and in-between widgets
          children: [
            buildFiller(),
            buildFiller(),
            buildDayWidget(day: 'Monday', key: dayKeys[0]),
            buildFiller(),
            buildFiller(),
            buildDayWidget(day: 'Tuesday', key: dayKeys[1]),
            buildFiller(),
            buildFiller(),
            buildDayWidget(day: 'Wednesday', key: dayKeys[2]),
            buildFiller(),
            buildFiller(),
            buildDayWidget(day: 'Thursday', key: dayKeys[3]),
            buildFiller(),
            buildFiller(),
            buildDayWidget(day: 'Friday', key: dayKeys[4]),
            buildFiller(),
            buildFiller(),
            buildDayWidget(day: 'Saturday', key: dayKeys[5]),
            buildFiller(),
            buildFiller(),
            buildDayWidget(day: 'Sunday', key: dayKeys[6]),
            buildFiller(),
            buildFiller(),
          ],
        ),
      ),
      floatingActionButton: Column(children: [
        FloatingActionButton(
          onPressed: () => _scrollToDay(0),
          tooltip: 'Monday',
          child: const Text('1'),
        ),
        const SizedBox(height: 4,),
        FloatingActionButton(
          onPressed: () => _scrollToDay(1),
          tooltip: 'Tuesday',
          child: const Text('2'),
        ),
        const SizedBox(height: 4,),
        FloatingActionButton(
          onPressed: () => _scrollToDay(2),
          tooltip: 'Wednesday',
          child: const Text('3'),
        ),
        const SizedBox(height: 4,),
        FloatingActionButton(
          onPressed: () => _scrollToDay(3),
          tooltip: 'Thursday',
          child: const Text('4'),
        ),
        const SizedBox(height: 4,),
        FloatingActionButton(
          onPressed: () => _scrollToDay(4),
          tooltip: 'Friday',
          child: const Text('5'),
        ),
        const SizedBox(height: 4,),
        FloatingActionButton(
          onPressed: () => _scrollToDay(5),
          tooltip: 'Saturday',
          child: const Text('6'),
        ),
        const SizedBox(height: 4,),
        FloatingActionButton(
          onPressed: () => _scrollToDay(6),
          tooltip: 'Sunday',
          child: const Text('7'),
        ),
      ],
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.end,
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    );
  }

  void _scrollToDay(int day) {
    // scroll to a specific day
    Scrollable.ensureVisible(dayKeys[day].currentContext!);
  }

  Widget buildDayWidget({required String day, required GlobalKey key}) {
    return ListTile(
      key: key,
      title: Text(day),
      subtitle: Text('This is a widget giving you an overview of the different tasks of $day', softWrap: true,),
    );
  }

  Widget buildFiller() {
    return Container(
      color: Color.fromARGB(255, Random().nextInt(255), Random().nextInt(255), Random().nextInt(255)),
      width: double.maxFinite,
      height: 200,
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM