轻敲或释放TextField小部件时重建Flutter小部件

问题描述 投票:0回答:1

我面临的问题是,每当我点击textField时,小部件都会重新生成。以及释放键盘时]

我已经检查了this answer,但是它不起作用。

这里是小部件

class ListSearch extends StatefulWidget {
  @override
  _ListSearchState createState() _ListSearchState();
}

class _ListSearchState extends State<ListSearch> {
   @override
  Widget build(BuildContext context) { print('rebuild....'); 
    return MaterialApp(
        theme: ThemeData(fontFamily: "Lato"),
        home: Scaffold(
          appBar: AppBarBuilder.getAppBar(context, "Listings", null),
          body: SingleChildScrollView(
            child: Column(
              children: <Widget>[
                TextField(
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (val) {applyFilter(val); },
                cursorColor: Colors.blue,
                controller: searchController,
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 16.0,
                ),
                decoration: InputDecoration(
                  focusedBorder: InputBorder.none,
                  border: InputBorder.none,
                  alignLabelWithHint: false,
                  hintText: "Search",
                ),
              ),
                Container(height: 600.0, child: buildLists()),
              ],
            ),
          ),
        ),
    );
  }
}

对此有任何解决方案?

更新

同样,当我导航到此页面时,它也会生成两次

Navigator.of(context).push(MaterialPageRoute(builder: (context) => ListSearch()));
flutter flutter-layout
1个回答
1
投票

检查您的代码,直到热重新加载后它才重新构建窗口小部件applyFilter方法,如果您正在使用setstate,它将重新加载小部件

import 'package:flutter/material.dart';

class ListSearch extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return ListSearchState();
  }
}

class ListSearchState extends State<ListSearch> {

  TextEditingController _searchController = TextEditingController();
  @override
  Widget build(BuildContext context) {
   print('rebuild....');
  return MaterialApp(
    debugShowCheckedModeBanner: false,
    theme: ThemeData(fontFamily: "Lato"),
    home: Scaffold(
      appBar: AppBar(
        title: Text("Listing"),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[

           TextField(
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (val) {
                  //applyFilter(val);
                },
                cursorColor: Colors.blue,
                controller: _searchController,
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 16.0,
                ),
                decoration: InputDecoration(
                  focusedBorder: InputBorder.none,
                  border: InputBorder.none,
                  alignLabelWithHint: false,
                  hintText: "Search",
                ),
              ),

            Container(height: 600.0, child: Text("Define you list widget")
           // buildLists()
            ),
          ],
        ),
      ),
    ),
  );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.