You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter实现点击FAB自动滚动到Text组件中指定字符串「Chorus:」的解决方案

Solution: Auto-Scroll to "Chorus:" in Your Flutter Lyrics App

Got it, let's get that auto-scroll feature working for your lyrics app! Here's a step-by-step implementation that will make your FAB jump straight to the "Chorus:" section whenever it's tapped.

Step 1: Add Required Controllers and Keys

First, we need a ScrollController to control the SingleChildScrollView, and a GlobalKey to access the layout information of your Text widget. Add these to your state class:

final ScrollController _scrollController = ScrollController();
final GlobalKey _textKey = GlobalKey();

Step 2: Bind Controllers/Keys to Your Widgets

Update your SingleChildScrollView and Text components to use the controller and key we just defined:

@override
Widget build(BuildContext context) {
  return Scaffold(
    extendBody: true,
    body: SafeArea(
      child: SingleChildScrollView(
        controller: _scrollController, // Attach the scroll controller
        physics: const BouncingScrollPhysics(),
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10),
          child: Text(
            kTheAstronomers,
            key: _textKey, // Attach the global key
            style: const TextStyle(
              fontSize: 30,
              fontFamily: 'Montserrat',
              fontWeight: FontWeight.w600,
            ),
          ),
        ),
      ),
    ),
    floatingActionButton: FloatingActionButton(
      onPressed: autoScrollToChorus,
      child: const Icon(Icons.skip_next), // Use any icon you prefer
    ),
  );
}

Step 3: Implement the Auto-Scroll Logic

Now, write the autoScrollToChorus method. This will calculate the exact position of "Chorus:" in your text and animate the scroll to that spot:

void autoScrollToChorus() async {
  // Wait a tiny bit to ensure the widget is fully rendered
  await Future.delayed(const Duration(milliseconds: 100));

  // Get the render box of the Text widget
  final RenderBox renderBox = _textKey.currentContext!.findRenderObject() as RenderBox;

  // Create a TextPainter to calculate text offsets (match your Text widget's style exactly!)
  final TextPainter textPainter = TextPainter(
    text: TextSpan(
      text: kTheAstronomers,
      style: const TextStyle(
        fontSize: 30,
        fontFamily: 'Montserrat',
        fontWeight: FontWeight.w600,
      ),
    ),
    textDirection: TextDirection.ltr,
  )..layout(maxWidth: renderBox.size.width);

  // Find the starting index of "Chorus:" in the lyrics
  final chorusStartIndex = kTheAstronomers.indexOf('Chorus:');
  if (chorusStartIndex == -1) return; // Exit if "Chorus:" isn't found

  // Get the vertical offset of the "Chorus:" text
  final chorusOffset = textPainter.getOffsetForCaret(
    TextPosition(offset: chorusStartIndex),
    Rect.zero,
  );

  // Animate the scroll to the chorus position
  _scrollController.animateTo(
    chorusOffset.dy,
    duration: const Duration(milliseconds: 500), // Smooth animation duration
    curve: Curves.easeInOut, // Nice easing curve for natural feel
  );
}

Key Notes:

  • Match Text Styles: Make sure the TextSpan style in TextPainter is identical to your Text widget's style. If they differ, the calculated offset will be incorrect.
  • Handle Missing "Chorus:": The code checks if "Chorus:" exists in the text to avoid errors if it's missing from some lyrics.
  • Wait for Render: The small delay ensures the widget is fully built before we try to access its render box. For a more precise approach, you could use WidgetsBinding.instance.addPostFrameCallback instead of the delay.

That's it! Now tapping your FAB will smoothly scroll your lyrics straight to the chorus section, saving users the hassle of manual scrolling.

内容的提问来源于stack exchange,提问作者public static void Main

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 15:27:34