Search code examples
flutterfiltertagsinput-fieldflutter-packages

make filter Widget with Flutter-Packages


I have just started learning the Flutter and this is my first Question in Stackoverflow! I am trying to write filter Widget with help of Filter-List Package. I want to have a Input field in addition to the searchfield , but by adding the Textfield command via Column or Container the search field will be also removed.any suggestion how can i add Input-field into the Following code? Or maybe should I use another package...?

enter image description here

import 'package:flutter/material.dart';
import 'package:filter_list/filter_list.dart';
    
class FilterPage extends StatefulWidget {
  const FilterPage({Key key, this.allTextList}) : super(key: key);
  final List<String> allTextList;
    
  @override
    _FilterPageState createState() => _FilterPageState();
}
    
class _FilterPageState extends State<FilterPage> {
  @override
    Widget build(BuildContext context) {
      List<String> countList = [
          "A",
          "M",
          "p",
          "kl",
          "D",
          "T",
          "c",
          "ST ",
          "M"
      ];
      return Scaffold(
        appBar: AppBar(
          title: Text("Filter list Page"),
        ),
        body: SafeArea(
          child: FilterListWidget(
            allTextList: countList,
            height: MediaQuery.of(context).size.height,
            hideheaderText: true,
            selectedTextBackgroundColor: Colors.red,
            applyButonTextBackgroundColor: Colors.red,
            allResetButonColor: Colors.grey,
            onApplyButtonClick: (list) {
              Navigator.pop(context, list);
            },
          ),
        ),
      );
  }
}
    
      
<<MAIN Dart>>>
import 'package:flutter/material.dart';
import 'filter.dart';

void main() async {
  runApp(MyApp());
}
    
class MyApp extends StatelessWidget {
  @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.red,
        ),
        debugShowCheckedModeBanner: false,
        home:FilterPage(),
      );
  }
}

Solution

  • You can copy paste run full code below
    You can use Expanded wrap FilterListWidget and TextField and provide flex
    code snippet

    body: SafeArea(
            child: Column(
              children: [
                Expanded(
                  flex: 4,
                  child: FilterListWidget(
                    allTextList: countList,
                   ...
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: TextField(
    

    working demo

    enter image description here

    full code

    import 'package:flutter/material.dart';
    import 'package:filter_list/filter_list.dart';
    
    class FilterPage extends StatefulWidget {
      const FilterPage({Key key, this.allTextList}) : super(key: key);
      final List<String> allTextList;
    
      @override
      _FilterPageState createState() => _FilterPageState();
    }
    
    class _FilterPageState extends State<FilterPage> {
      @override
      Widget build(BuildContext context) {
        List<String> countList = ["A", "M", "p", "kl", "D", "T", "c", "ST ", "M"];
        return Scaffold(
          appBar: AppBar(
            title: Text("Filter list Page"),
          ),
          body: SafeArea(
            child: Column(
              children: [
                Expanded(
                  flex: 4,
                  child: FilterListWidget(
                    allTextList: countList,
                    height: MediaQuery.of(context).size.height,
                    hideheaderText: true,
                    selectedTextBackgroundColor: Colors.red,
                    applyButonTextBackgroundColor: Colors.red,
                    allResetButonColor: Colors.grey,
                    onApplyButtonClick: (list) {
                      //Navigator.pop(context, list);
                    },
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: TextField(
                    decoration: InputDecoration(
                        labelText: 'Price'
                    ),
                  ),
                )
              ],
            ),
          ),
        );
      }
    }
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            title: 'Flutter Demo',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: FilterPage());
      }
    }