ArcticZeroo / flutter-search-bar

(mostly) Automatic search-enabled appBar for flutter
BSD 3-Clause "New" or "Revised" License
266 stars 69 forks source link

Error when I run the example code #3

Closed ride4sun closed 6 years ago

ride4sun commented 6 years ago

The context in the example is null Scaffold.of(context) .showSnackBar(new SnackBar(content: new Text('You wrote $value!'))

when I run the example code. This is with the latest flutter and libs. I copied the example code to a newly generated flutter app.

More info here: https://docs.flutter.io/flutter/material/Scaffold/of.html

This is fixing it:

import 'package:flutter/material.dart';
import 'package:flutter_search_bar/flutter_search_bar.dart';

void main() {
  runApp(new SearchBarDemoApp());
}

class SearchBarDemoApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: 'Search Bar Demo',
        theme: new ThemeData(primarySwatch: Colors.blue),
        home: new SearchBarDemoHome());
  }
}

class SearchBarDemoHome extends StatefulWidget {
  @override
  _SearchBarDemoHomeState createState() => new _SearchBarDemoHomeState();
}

class _SearchBarDemoHomeState extends State<SearchBarDemoHome> {
  SearchBar searchBar;
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  AppBar buildAppBar(BuildContext context) {
    return new AppBar(
        title: new Text('Search Bar Demo'),
        actions: [searchBar.getSearchAction(context)]);
  }

  void onSubmitted(String value) {
    setState(() => _scaffoldKey.currentState
        .showSnackBar(new SnackBar(content: new Text('You wrote $value!'))));
  }

  _SearchBarDemoHomeState() {
    searchBar = new SearchBar(
        inBar: false,
        buildDefaultAppBar: buildAppBar,
        setState: setState,
        onSubmitted: onSubmitted);
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: searchBar.build(context),
      key: _scaffoldKey,
      body: new Center(
          child: new Text("Don't look at me! Press the search button!")),
    );
  }
}
ArcticZeroo commented 6 years ago

Good catch! I can't make the commit for a few days, can you open a PR?

ride4sun commented 6 years ago

Sure,will do

On Oct 25, 2017 08:09, "Spencer" notifications@github.com wrote:

Good catch! I can't make the commit for a few days, can you open a PR?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ArcticZeroo/flutter-search-bar/issues/3#issuecomment-339362573, or mute the thread https://github.com/notifications/unsubscribe-auth/AHYc61QAEuO2LgN04CecAA6HvY-9otchks5sv08cgaJpZM4QFOlO .

ride4sun commented 6 years ago

Sure, will do

On Oct 25, 2017 08:09, "Spencer" notifications@github.com wrote:

Good catch! I can't make the commit for a few days, can you open a PR?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ArcticZeroo/flutter-search-bar/issues/3#issuecomment-339362573, or mute the thread https://github.com/notifications/unsubscribe-auth/AHYc61QAEuO2LgN04CecAA6HvY-9otchks5sv08cgaJpZM4QFOlO .