簡體   English   中英

[Flutter][Web] - 當我們點擊任何其他小部件時,如何限制從文本字段中移除焦點?

[英][Flutter][Web] - How to restrict the focus removing from the text field when we tap on any other widgets?

在下面的簡單示例中,我有 Textfields 以及 Text 和 Container 小部件。 如果我們在焦點位於文本字段中時鼠標點擊容器或文本小部件,則文本字段焦點將丟失。

在這種情況下,我希望焦點始終保留在文本字段中,直到點擊任何其他可聚焦的小部件,即另一個文本字段。 有什么建議嗎? 我怎樣才能做到這一點?

import 'package:flutter/material.dart';

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

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

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

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {
  FocusNode focusNode = FocusNode();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(icon: const Icon(Icons.menu), onPressed: (() {})),
          IconButton(icon: const Icon(Icons.remove), onPressed: (() {}))
        ],
        title: const Text('Flutter Demo Home Page'),
      ),
      body: Center(
          child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
            const TextField(),
            const Text('Home Page'),
            Container(
                color: Colors.amberAccent,
                width: 400,
                height: 100,
                child: const Center(child: Text('Container'))),
            const TextField(),
          ])),
    );
  }
}

謝謝!

除非每次關閉鍵盤時都重新激活焦點模式,否則無法保持焦點固定

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM