【问题标题】:How to force focus on an image once page loads with Flutter?使用 Flutter 加载页面后如何强制关注图像?
【发布时间】:2021-05-18 16:50:03
【问题描述】:

我似乎找不到关于在页面初始加载时关注除 TextField 以外的任何内容的文档。

我有一个条款和条件页面,我试图让页面上的第一项(作为徽标)首先关注。相反,当您第一次向右滑动时,第一个焦点会突出显示容器、徽标、段落文本和操作按钮。一旦成为焦点,它就会首先读取段落文本而不是徽标。

我的目标是防止将焦点放在容器上并直接进入徽标。所以不要只关注容器中的物品。从那里焦点将转到段落文本,然后是操作按钮。

这是我的代码:

landing_page.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:spectrum_access_flutter/views/widgets/buttons/negative_button.dart';
import 'package:spectrum_access_flutter/views/widgets/buttons/primary_button.dart';
import 'package:spectrum_access_flutter/views/widgets/buttons/secondary_button.dart';

class LandingPage extends StatefulWidget {
  static const path = 'Terms';

  @override
  _LandingPageState createState() => _LandingPageState();
}

class _LandingPageState extends State<LandingPage> {
  // Define the focus node. To manage the lifecycle, create the FocusNode in
  // the initState method, and clean it up in the dispose method.
  FocusNode myFocusNode;
  bool isVisible = false;

  @override
  void initState() {
    super.initState();

    myFocusNode = FocusNode();
  }

  @override
  void dispose() {
    // Clean up the focus node when the Form is disposed.
    myFocusNode.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: Center(
      child: new ListView(
          shrinkWrap: true,
          padding: new EdgeInsets.all(25.0),
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(5.0),
                  child: Image.network(
                    ''
                    'https://upload.wikimedia.org/wikipedia/commons/1/17/Google-flutter-logo.png',
                    semanticLabel: 'Flutter Logo',
                    width: 270.0,
                    focusNode: myFocusNode
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(10, 30.0, 10, 30.0),
                  child: Align(
                    alignment: Alignment.center,
                    child: Container(
                      child: Text(
                        'To use Spectrum Access, please agree to the terms of service',
                        textAlign: TextAlign.center,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 5.0),
                  child: SecondaryButton(
                      title: 'Terms and Conditions',
                      semanticLabel: 'Terms and Conditions',
                      onPressed: () {
                        print('Clicked Terms!');
                      }),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 5.0),
                  child: SecondaryButton(
                      title: 'Terms and Conditions',
                      semanticLabel: 'Privacy Policy',
                      onPressed: () {
                        print('Clicked Privacy!');
                      }),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(0, 30.0, 0, 5.0),
                  child: PrimaryButton(
                      title: 'Agree',
                      onPressed: () async {
                        print('Clicked Agree!');
                      }),
                ),
                Padding(
                  padding: const EdgeInsets.fromLTRB(0, 0, 0, 5.0),
                  child: NegativeButton(
                      title: 'Disagree',
                      onPressed: () {
                        print('Clicked Disagree!');
                      }),
                ),
              ],
            )
          ]),
    ));
  }
}

上线53focusNode: myFocusnode 给我这个错误:“未定义命名参数'focusNode'。”

遵循这些说明 --> https://flutter.dev/docs/cookbook/forms/focus

【问题讨论】:

    标签: flutter dart flutter-ios flutter-android


    【解决方案1】:

    我能够使用IndexedSemantics 完成此操作,并将索引号 -1 放在 ListView 容器上,并为子项设置索引顺序。这解决了我的问题:

    
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'package:spectrum_access_flutter/views/widgets/buttons/negative_button.dart';
    import 'package:spectrum_access_flutter/views/widgets/buttons/primary_button.dart';
    import 'package:spectrum_access_flutter/views/widgets/buttons/secondary_button.dart';
    
    class LandingPage extends StatefulWidget {
      static const path = 'Terms';
    
      @override
      _LandingPageState createState() => _LandingPageState();
    }
    
    class _LandingPageState extends State<LandingPage> {
      bool isVisible = false;
    
      @override
      void initState() {
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            body: Center(
                child: IndexedSemantics(
          index: -1,
          child: new ListView(
              shrinkWrap: true,
              padding: new EdgeInsets.all(25.0),
              addSemanticIndexes: false,
              semanticChildCount: 6,
              children: [
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Padding(
                        padding: const EdgeInsets.all(5.0),
                        child: IndexedSemantics(
                          index: 0,
                          child: Semantics(
                              focused: true,
                              child: Image.network(
                                ''
                                'https://upload.wikimedia.org/wikipedia/commons/1/17/Google-flutter-logo.png',
                                semanticLabel: 'Flutter Logo',
                                width: 270.0,
                              )),
                        )),
                    Padding(
                        padding: const EdgeInsets.fromLTRB(10, 30.0, 10, 30.0),
                        child: IndexedSemantics(
                          index: 0,
                          child: Align(
                            alignment: Alignment.center,
                            child: Container(
                              child: Text(
                                'To use Spectrum Access, please agree to the terms of service',
                                textAlign: TextAlign.center,
                              ),
                            ),
                          ),
                        )),
                    Padding(
                        padding: const EdgeInsets.only(top: 5.0),
                        child: IndexedSemantics(
                          index: 0,
                          child: SecondaryButton(
                              title: 'Terms and Conditions',
                              semanticLabel: 'Terms and Conditions',
                              onPressed: () {
                                print('Clicked Terms!');
                              }),
                        )),
                    Padding(
                        padding: const EdgeInsets.only(top: 5.0),
                        child: IndexedSemantics(
                          index: 0,
                          child: SecondaryButton(
                              title: 'Terms and Conditions',
                              semanticLabel: 'Privacy Policy',
                              onPressed: () {
                                print('Clicked Privacy!');
                              }),
                        )),
                    Padding(
                        padding: const EdgeInsets.fromLTRB(0, 30.0, 0, 5.0),
                        child: IndexedSemantics(
                          index: 0,
                          child: PrimaryButton(
                              title: 'Agree',
                              onPressed: () async {
                                print('Clicked Agree!');
                              }),
                        )),
                    Padding(
                        padding: const EdgeInsets.fromLTRB(0, 0, 0, 5.0),
                        child: IndexedSemantics(
                          index: 0,
                          child: NegativeButton(
                              title: 'Disagree',
                              onPressed: () {
                                print('Clicked Disagree!');
                              }),
                        )),
                  ],
                )
              ]),
        )));
      }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2013-05-14
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多