1

I am using a bottom navigation bar with 3 pages. One of them (HomePage) has a search icon. Every time I pressed on it, the keyboard appears for barely one second, then disappears and the app rebuilds itself. I tried other solutions online such as putting the future values in initState() but it did not work. Note that I have also implemented a sign up/sign in system. When the user has signed up/ signed in, they will be then redirected to the page which has a bottom navigation bar. (Also, I did some research, and apparently, only android phones are "suffering" from this problem when the flutter app becomes complex. I don't know how much it is true though)

Here is the code for the Bottom Navigation Bar

 class BottomNavigationPage extends StatefulWidget{
  final AfreecaInvestAccount _afreecaInvestAccount;
  _BottomNavigationPageState createState() => _BottomNavigationPageState();
  BottomNavigationPage(this._afreecaInvestAccount);
}

class _BottomNavigationPageState extends State<BottomNavigationPage> {
  
  static List<Widget> widgetOptions;
  int selectedIndex = 0;
  @override
  void initState() {
    widgetOptions = 
    [
      HomePage(widget._afreecaInvestAccount),
      StockSearchPage(),
      AccountPage(widget._afreecaInvestAccount)
    ];
    super.initState();
  }
  
  void onTabTapped(int index) {
    setState(() {
      selectedIndex = index;
    });
  }
  @override
  Widget build(BuildContext context) {
     return Scaffold(
       body: widgetOptions.elementAt(selectedIndex),
       bottomNavigationBar: _bottomNavigationBar(),
     );
  }
  
  BottomNavigationBar _bottomNavigationBar() {
    return BottomNavigationBar(
        items: const<BottomNavigationBarItem>
        [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
          BottomNavigationBarItem(icon: Icon(Icons.search), label: "Search Stocks"),
          BottomNavigationBarItem(icon: Icon(Icons.account_box), label: "Account"),
        ],
        currentIndex: selectedIndex,
        onTap: onTabTapped,
    );
  }
  
}

Here is the code for the Home Page:

class HomePage extends StatefulWidget {
  final AfreecaInvestAccount _afreecaInvestAccount;
  @override
  _HomePage createState() => _HomePage(_afreecaInvestAccount);
  HomePage(this._afreecaInvestAccount);
}

class _HomePage extends State<HomePage> {
  Future<List<int>> _futureBPandOSValue;
  final AfreecaInvestAccount _afreecaInvestAccount;
  FutureBuilder<List<int>> _futureBuilder;
  String _accessToken;
  List<String> list =
  [
    "TSLA",
    "AMZN",
    "GME",
    "AAPL"
  ];
  _HomePage(this._afreecaInvestAccount);
  @override
  void initState() {
    // TODO: implement initState
//    timer = Timer.periodic(Duration(seconds: 30), (timer) {build(context);});
     _accessToken = _afreecaInvestAccount.accessToken;
     _futureBPandOSValue = _futureBPAndOSValues(_accessToken);
     _futureBuilder = FutureBuilder<List<int>> (
       future: _futureBPandOSValue,
       builder: _BPandOSValueBuilder,
     );
     super.initState();
  }

  void buildWidget() {
    setState(() {

    });
  }


  @override
  Widget build(BuildContext context) {
//    String _selectedItem;

    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
        automaticallyImplyLeading: false,
        actions: [
        IconButton(
            icon: Icon(Icons.search),
            onPressed: () {
              showSearch(context: context, delegate: StockSearch());
            },
          )
        ],
        ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
//          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            _futureBuilder,
            SizedBox(height: 10.0,),
            Card(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: _returnStocksTable(context, _accessToken),
             /* _returnStocksTable method is not included in this code block because it is not the cause of the problem */
              )
            ),
        ],
       ),
     )
    );
  }


}

For this code, showSearch is already provided by flutter itself :

onPressed: () {
              showSearch(context: context, delegate: StockSearch());
            },

Here is the StockSearch() file:

class StockSearch extends SearchDelegate<String> {
  String accessToken = "djdjicjscnjdsncjs";
  List<String> stockList =
  [
    "TSLA",
    "AMZN",
    "GME",
    "AAPL"
  ];
  final recentStocks = ['TSLA'];
  @override
  List<Widget> buildActions(BuildContext context) {
    // actions for app bar
    return [
      IconButton(icon: Icon(Icons.clear), onPressed: () {
        query = "";
      })
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    // leading icon on the left of the app bar
    return IconButton(
        icon: AnimatedIcon(
          icon: AnimatedIcons.menu_arrow,
          progress: transitionAnimation,
      ),
      onPressed: () {
          close(context, null);
      }

    );
  }

  @override
  Widget buildResults(BuildContext context) {
    return Container();
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    // show when someone searches for stocks
    final suggestionList = query.isEmpty? recentStocks
        : stockList.where((stock) => stock.startsWith(query)).toList();

    return ListView.builder(
      itemBuilder: (context, index) => ListTile(
        onTap: () {
          WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
            Navigator.push(context, MaterialPageRoute(builder: (context) => TransactionPage(suggestionList.elementAt(index), accessToken)));
          });
        },
        title: RichText(text: TextSpan(
          text: suggestionList.elementAt(index).substring(0, query.length),
          style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold),
          children: [TextSpan(
            text: suggestionList.elementAt(index).substring(query.length),
            style: TextStyle(color: Colors.grey)
           )
          ]
        )
        ),
      ),
      itemCount: suggestionList.length,
    );
  }
}

Thank you in advance for your help!

  • You didn't share this code `showSearch(context: context, delegate: StockSearch());` which is most likely the reason for your problem. If you are building the search bar widget outside this screen, and passing to it the context of this screen, whenever you setState in that search widget, the state being referred to is taken from the `context` you are passing `onPressed`. Please post that and can give further details perhaps to help you out. – Huthaifa Muayyad Apr 09 '21 at 18:15
  • @HuthaifaMuayyad I just shared it. – cprogrammer Apr 09 '21 at 18:23
  • May I ask what is the purpose of using `onPressed: () { close(context, null); }`? – Huthaifa Muayyad Apr 09 '21 at 18:25
  • @HuthaifaMuayyad when a user presses on it, it returns back to the homepage. I commented it out and tested it just in case, but there is still the same problem. – cprogrammer Apr 09 '21 at 18:35
  • @cprogrammer where is your `buildWidget` method being used ? – Calvin Gonsalves Apr 09 '21 at 22:54
  • @CalvinGonsalves it is not used. I removed it and there is still the same problem. – cprogrammer Apr 10 '21 at 15:05

0 Answers0