颤音的形式步骤和滚动

发布于 2025-02-06 06:18:27 字数 2217 浏览 3 评论 0原文

我有形式和步进小部件的颤动视图。每个步进器都有很多小部件。当我单击步骤时,屏幕会自动滚动到新扩展的步骤的末尾。如何更改滚动以查看单击之后扩展的步骤的顶部?

(当我单击步骤2时,Step2扩展了,我从STEP2中看到了最后的小部件,而不是STEP2的第一个小部件)

代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

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

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

class _MyHomePageState extends State<MyHomePage> {
  int currentStep = 0;
  List<Step> generateSteps(int j) {
    List<Step> a = [];
    for (int i = 0; i < j; i++) {
      a.add(Step(
          title: const Text("STEP"),
          content: Column(children: generateWidgets(10))));
    }
    return a;
  }

  List<Widget> generateWidgets(int j) {
    List<Widget> a = [];

    for (int i = 0; i < j; i++) {
      a.addAll([
        Text("Field: $i"),
        TextFormField(
          decoration: const InputDecoration(
            hintText: 'Enter your email',
          ),
        ),
      ]);
    }
    return a;
  }

  @override
  Widget build(BuildContext context) {
    List<Step> ft = generateSteps(6);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Form(
            child: Stepper(
          currentStep: currentStep,
          onStepTapped: (int step) {
            setState(() {
              currentStep = step;
            });
          },
          steps: ft,
          physics: const NeverScrollableScrollPhysics(),
        )),
      ),
    );
  }
}

I have Flutter view with Form and Stepper widgets. Every Stepper have a lot of widgets inside. When I click on Step, screen is automatically scrolling to the end of new expanded Step. How to change scrolling to see top of expanded Step after click on it?

enter image description here

(When I click on STEP 2, STEP2 is expanded and I see last widgets from STEP2, not first widgets of STEP2)

Code:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;

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

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

class _MyHomePageState extends State<MyHomePage> {
  int currentStep = 0;
  List<Step> generateSteps(int j) {
    List<Step> a = [];
    for (int i = 0; i < j; i++) {
      a.add(Step(
          title: const Text("STEP"),
          content: Column(children: generateWidgets(10))));
    }
    return a;
  }

  List<Widget> generateWidgets(int j) {
    List<Widget> a = [];

    for (int i = 0; i < j; i++) {
      a.addAll([
        Text("Field: $i"),
        TextFormField(
          decoration: const InputDecoration(
            hintText: 'Enter your email',
          ),
        ),
      ]);
    }
    return a;
  }

  @override
  Widget build(BuildContext context) {
    List<Step> ft = generateSteps(6);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Form(
            child: Stepper(
          currentStep: currentStep,
          onStepTapped: (int step) {
            setState(() {
              currentStep = step;
            });
          },
          steps: ft,
          physics: const NeverScrollableScrollPhysics(),
        )),
      ),
    );
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文